I defined the request type as "any" but I realized it shouldn't be "any" and it also can't be "Request". I need to extend express-session to create a custom session type. How can I do that, and add the following information to the request session?
access_token : string
userinfo: {
sub: 'Diur4_PcorTDFMRP99pgP9Qwkclxg4',
name: 'John Kay',
family_name: 'Kay',
given_name: 'John',
picture: 'https://graph.microsoft.com/v1.0/me/photo/$value',
email: '[email protected]'
}
isAuth:boolean
req.session.access_token = access_token;
req.session.userinfo = Userinformtion;
req.session.isAuth = true; //save session
const isAuth = (req:any, res: Response, next: NextFunction) =\> {
if (req.session.isAuth) {
next();
} else {
res.render("landing");
}
};
In order to add
extra session datato anExpress session, you would simply define it like you did in your writeup. Here's a working example I use:This is a simple route handler for an express endpoint. If you
POSTtimezoneand the end-user is signed-in, then their session will now contain their timezone. No extra libraries are necessary for that, justexpressandexpress-session.