When using passportjs with nodejs, I get a 504 error when trying to insert into the database.
The mongo database is hosted externally from the application server and the application is behind a reverse proxy (the reverse proxy converts the application port to port 80).
The issue only happens when trying to insert into a mongo document:
passport.use(new GoogleStrategy({
clientID: "client_id",
clientSecret: "client_secret",
callbackURL: "oauth_url_callback"
},
function(accessToken, refreshToken, profile, done) {
passport.serializeUser(function(user,done){
mongo.connect(url, function(err,db){
if(err){
console.log(err, ' was the err');
fs.open('views/debug/debug.txt', 'rw', 'a+', function(err,data){
fs.writeFileSync('views/debug/debug.txt', err);
});
done(err);
} else {
db.collection('users').find({profile:profile}, function(err, user) {
if (err) {
fs.open('views/debug/debug.txt', 'rw', 'a+', function(err,data){
fs.writeFileSync('views/debug/debug.txt', err);
});
db.collection('users').insertOne({profile:profile, name:"John Doe"}, function(err, user){
if(err){
console.log("failed to insert", err);
} else {
console.log('Registered a new user!');
}
});
} else {
console.log('We have a returning user!');
}
console.log('testing');
fs.open('views/debug/debug.txt', 'rw', 'a+', function(err,data){
fs.writeFileSync('views/debug/debug.txt', err);
});
done(null, user);
});
}
});
});
}
));
I am not sure why this happens, but it only happens when you try to insert into the database. When I refresh the page, it doesn't 504 because it returns a error saying the token has expired.
I also tried logging for errors, and there seems to be no errors. I start the process with both stdout and stderror as arguments to extract output but to no avail.
It also doesn't insert the document/item into the db which is strange.
See Question&Answers more detail:
os