Routes.js
app.get('/auth/google/redirect', passport.authenticate('google'), (req, res) => {
console.log('req.user:', req.user) //>>>>>Outputs {username: 'bob', id: '..and so on'...}
res.redirect('/profile/test')
})
profileRoutes.js
app.get('/profile/test', (req, res) => {
res.send('This is the req.user: ' + req.user);
})
Output: "This is the req.user:
undefined
"
Note that I succesfully was able to access the req.user in the routes.js
before redirecting to /profile/test
, why is the req.user
variable from route.js
not passed along to the /profile/test
?
If I doesn't redirect, then and just send the req.user, it works as expected:
app.get('/auth/google/redirect', passport.authenticate('google'), (req, res) => {
res.send(req.user)
})
OutPut: {"_id":"5b8d42186cfd139c00c42c9","username":"Ryan Cameron","googleId":"11280041518849062335","__v":0}
I was following theNetNinja's tutorial on youtube, and it worked for him. So it's weird why the variable isn't being passed to the new redirected route for me but it does for him. Watch it work for him
Why is the req.user
variable not passed to the redirected route?
I can pass the variable in a query string to the redirected route but that would not be secure. The guy in the video didn't do that, he had the same code as me above. But for some reason it doesn't work for me. Why?