Closed jmcilhargey closed 1 year ago
Looks like passport.serialize
just wipes out everything else on the session including other keys that may be part of the session (I was trying to set request.session.oAuth
to store additional credentials..)
A work around could be to set additional optional properties inside of the serialize
function.. But I feel like passport should allow other things to be set on the session outside of it's own property..
Here's the workaround for example with Google and local auth - But if you are using something outside of Passport it looks like it's not possible to use the express-session
with passport
bc it gets wiped out?
passport.serializeUser<UserSessionData>((user, done) => {
process.nextTick(() => {
const { id, email, firstName, lastName, roles, verified, google } = user; // Serialize google data too if using Google Oauth2
done(null, { id, email, firstName, lastName, roles, verified, google });
});
});
interface UserSessionData {
id: number;
email: string;
firstName: string;
lastName: string;
roles: string[];
verified: boolean;
google?: {
accessToken: string;
refreshToken: string;
expiresAt: number;
};
}
Okay.. After inspecting the source code I see the merge
function and I found the answer to my question - Make sure that keepSessionInfo: true
is set for all Passport strategies!
export function googleRoute(router: IRouter) {
router.get(
GoogleRoutes.Auth,
passport.authenticate('google', {
scope: GOOGLE_LOGIN_SCOPES,
accessType: 'offline',
prompt: 'consent',
keepSessionInfo: true,
})
)
router.get(
GoogleRoutes.Callback,
passport.authenticate('google', {
failureRedirect: AppRoutes.Login,
keepSessionInfo: true,
}),
function (request, response) {
response.redirect(AppRoutes.Home)
}
)
}
What
Can't persist
request.session
data inside of theGoogleStrategy
callback using the ExpressRequest
object argument..Expected
request.session.oAuth.google
contains the persisted session data for subsequent requestsActual
request.session.oAuth.google
isundefined
. Therequest.session.save()
does not save the updated session to the store..Logging the
request.session
before and after callingsave()
indicates the property is attached to the request object during that request context.. but is not persisted.I'm able to persist changes to session store for other OAuth strategies I'm using outside of Passport calling
request.session.save()
on this same server..Am I missing something?
Steps to reproduce
Server:
Session Middleware:
Passport Middleware:
Google Session Setter:
Environment
"engines": { "node": ">=16.0.0", "npm": ">=7.0.0" }, "passport": "^0.6.0", "passport-google-oauth20": "^2.0.0", "passport-local": "^1.0.0", "passport": "^0.6.0",