Node.js Express app with Google oauth not redirecting
I've tried to copy the login/registration workflow found in StackExchange
(with an added step of user validation), using passport and Google oauth
in a node.js Express application. The intended routing flow is this:
GET /
|
REDIR /users/login ("click here to login")
|
GET /auth/google
|
GET /auth/google/callback
|
user.status
|
+----"new"--------------+-------------"validated"----+
| | |
REDIR /users/oauthconfirm "pending" GET /
| |
REDIR /users/login REDIR /users/login
Code to do this:
// app.js
passport.use(new GoogleStrategy({
clientID: config.google_client_id,
clientSecret: config.google_client_secret,
callbackURL: "/auth/google/callback"
},
function (accessToken, refreshToken, profile, done) {
process.nextTick(function () {
db.db.collection("users", function (err, collection) {
if (err) throw err;
collection.findOne({id: profile.id}, function (err, record) {
console.log("Queried for user and found", record);
if (record) return done(null, record);
profile.status = "new";
done(null, profile);
});
});
});
})
);
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/users/login' }),
function (req, res) {
console.log("User status is", req.user.status);
switch (req.user.status) {
case "validated":
res.redirect('/'); break;
case "new":
res.redirect('/users/oauthconfirm'); break;
case "pending":
res.redirect('/users/login'); break;
}
}
);
app.get('/users/oauthconfirm', routes.users.oauthconfirm);
// routes/users.js
exports.oauthconfirm = function(req, res) {
console.log("In oauthconfirm...");
db.db.collection("users", function (err, collection) {
if (err) throw err;
collection.insert(req.user, function (err, records) {
if (err) throw err;
res.render('login', {messages: [{status: "success", text:
"Thank you. You will receive an e-mail when your account is
validated."}]});
});
});
};
Q: This was working fine on my dev server, but on my staging server, I'm
never getting the redirect to /users/oauthconfirm. Is there a code
problem? Could there be an issue arising from node.js being behind an
iptables port redirect on this server? A typical log looks like this:
GET / 302
GET /users/login 200
GET /auth/google 302
Queried for user and found null
User status is new
GET /auth/google/callback?... 302
GET /auth/google 302
User status is new
(... loop)
Thanks!
No comments:
Post a Comment