I tried to add Passport.JS for social login services that store confidential data (user_id) in session (express-session) and share to the client via cookie.
I can retrieve user_id from session from general route (like ‘/’, ‘/api’).
app.get('/api/current_user', (req, res,next)=>{
console.log(req.session)
if(!req.user){
res.send('Guess Login')
}else{
res.send(`current user id ${req.user.name}`)
}
})```
The output of the console log from the above code as follow.
**Session {**
** cookie: {**
** path: '/',**
** _expires: 2022-11-07T13:28:48.585Z,**
** originalMaxAge: 86400000,**
** httpOnly: true,**
** secure: false**
** },**
** passport: { user: '102012197665909713239' }**
**}**
So, I thought the session part look fine. Then, I tried to pass into the Apollo Server via context.
app.use(‘/graphql’,
cors(),
bodyParser.json(),
expressMiddleware(graphQLServer, {
context: async ({req, res}) => ({ req, res, session: req.session })
}))
Unfortunately, it didn't pass user(passport) to Apollo as I tried to access in resolver. Please see the result below.
console.log(context.session)
**Session {**
** cookie: {**
** path: '/',**
** _expires: 2022-11-07T13:28:52.830Z,**
** originalMaxAge: 86400000,**
** httpOnly: true,**
** secure: false**
** }**
**}**
References:
https://www.passportjs.org/packages/passport-google-oauth20/
https://www.apollographql.com/docs/apollo-server/api/express-middleware
Did I do something wrong?