0
votes

I have been trying to figure this out for 2 days now.

when I go to this route '/profile' deserializeUser works fine

However, when the user authinticate and is redirected back to the callback url, deserialzeUser is not called.

my redirect:

router.get('/discord', passport.authenticate('discord'));
//when the user authorize from discord page:

router.get('/discord/redirect', passport.authenticate('discord', { 
    failureRedirect: '/forbidden',
    successRedirect: '/profile'
}));

the user gets redirected to '/profile' which checks if they are authenticated (which will be false since req.user is empty due to deserializeUser not being called) then they will be required to authenticate again which is happening because deserializeUser is not being called.

Profile Route:

function isAuthorized(req, res, next) {
    if(req.user) {
        console.log("User is logged in.");
        console.log(req.user);
        next();
    }
    else {
        console.log("User is not logged in.");
        res.redirect('/auth/discord');
    }
}

router.get('/profile', isAuthorized, (req, res) => {
    console.log(req.user);
    res.send('You are logged in =) !');
});

I am using postgress database from Heroku+ express-session + passport-discord my session:

// Passport store session
const sessionConfig = {
  store: new pgSession({
      pool: db,
      tableName: 'session'
  }),
  name: 'SID',
  secret:process.env.SECRET,
  resave: false,
  saveUninitialized: false,
  cookie: {
      maxAge: 1000 * 60 * 60 * 24 * 7,
      sameSite: true,
      secure: false // ENABLE ONLY ON HTTPS
  }};
app.use(session(sessionConfig))
app.use(passport.initialize());
app.use(passport.session());
1

1 Answers

0
votes

I found my mistake.

Just remove

sameSite: true,

from the sessionconfig