https://github.com/harshitKyal/loginWithGoogle
this is my github repo link. I am using passport js to login with google . I created client key and secret key at google developer console. On running the code it is redirecting me to success page but req.user object is coming undefined. Please guide me through this. I took the code from https://github.com/mstade/passport-google-oauth2/tree/master/example
app.get( '/auth/google/callback',
passport.authenticate( 'google', {
successRedirect: '/',
failureRedirect: '/login'
}));
app.get('/', function(req, res){
res.render('index', { user: req.user });
});
This code redirecting me to index page . But In index page I have written
<% if (!user) { %>
<h2>Welcome! Please log in.</h2>
<% } else { %>
<h2>Hello, <%= user.displayName %>.</h2>
<% } %>
It should display Hello and name of the user.
But instead of that , it is displaying Welcome!Please log in.
The codebase looks good. Noticed that you're using connection to redis server for storing session info:
store: new RedisStore({
host: 'localhost',
port: 6379
}),
did you start it? To check if your redis server is running and your application can connect to it, you can have a look to debug output:
export DEBUG=*
//and then run your app
node example/app.js
If you'll see a lot of errors, like:
connect:redis Redis returned err { Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at Object.exports._errnoException (util.js:1020:11)
at exports._exceptionWithHostPort (util.js:1043:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
Then, your redis connection is missing.
To fix this you can either:
a) install and run redis locally
b) Use a docker image. This requires docker to be installed. I did try it on MAC OS Sierra, it works well with command:
docker run --name some-redis -p 6379:6379 -d redis
Looks like this is the thing you've missed - start redis server. If you set it up - everything should work well.