passport-slack-oauth2 icon indicating copy to clipboard operation
passport-slack-oauth2 copied to clipboard

`req.user` is undefined after triggering valid callback with `passport.authenticate`

Open Zeaneth opened this issue 1 year ago • 0 comments

Hi, I’m having an issue authenticating users and thought this might be a known problem where you could give some guidance.

After following the steps to authenticate the user, allow my Slack App access to the requested permissions, and then get to the callback URL,

app.get('/auth/slack/callback',
  passport.authenticate('Slack', { failureRedirect: '/login' }),
  (req, res) => res.redirect(HOME_URL)
);

I can access all the values and use them to find the user, so I get to the point where the callback done(null, user) gets triggered, I then get redirected to my HOME_URL, but the value of req.user is undefined, so I can’t sign in.

My current strategy is

const SlackStrategy = require('passport-slack-oauth2').Strategy;

passport.use(
    new SlackStrategy(
      {
        clientID: CLIENT_ID,
        clientSecret: CLIENT_SECRET,
        scope: ['identity.basic', 'identity.email']
      },
      (accessToken, refreshToken, profile, done) => {
        User.findOne({ email: profile.user.email })
          .then(user => {
            if (user) {
              return done(null, user);
            }
            return done(null, false);
          })
          .catch(err => {
            return done(err, false);
          });
      },
    ),
  );

I’ve also checked that both passport.serializeUser and passport.deserializeUser functions are not triggered after being redirected, so my hypothesis is that this is expected since the condition to call both requires a valid req.user value.

As context, I have successfully implemented the Google Strategy (with passport-google-oauth2) to sign in users, and even though both configurations get to the point where they trigger the callback done(null, user) -and it's the same user object in both cases, with the same passport and passport-oauth2 dependencies and versions-, I only get the req.user with Google (both passport.serializeUser and passport.deserializeUser are triggered after the callback).

Do you have any ideas why this could be happening?

Zeaneth avatar May 31 '23 05:05 Zeaneth