I’m working an application where Google login (Oauth) callback fails. When I start the app, it redirects me to the Google Oauth’s login page. Below is the code:
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:8080/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
userProfile=profile;
return done(null, userProfile);
}
));
app.get('/auth/google',
passport.authenticate('google', { scope : ['profile', 'email'] }));
app.get('/auth/google/callback',
// passport.authenticate('google', { failureRedirect: '/error' }),
passport.authenticate('google'),
function(req, res) {
// Successful authentication, redirect success.
var request = require("request");
var options = {
method: "POST",
url: "http://localhost:8082/web/google-callback",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(userProfile),
};
request(options, function (error, response) {
if (error) {
console.log(error);
return;
}
if (response.body != undefined){
res.redirect('/select?t=' + JSON.parse(response.body)['token']);
}
else{
res.redirect('/404');
}
});
});
After providing credential, callback fails and the response.body contains the following:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /web/google-callback</pre>
</body>
</html>
As stated in the response body there is an error in body. The response.redirect implementation is:
res.redirect('/select?t=' + JSON.parse(response.body)['token']);
An leads in this direction will be of great help.
Thanks in advance.