Miha Šušteršič Miha Šušteršič - 24 days ago 10
Javascript Question

passportJS - passport-facebook cannot GET failureRedirect route

Following the passportJS docs for passport-facebook I am trying to implement express middleware for facebook authentication. I am using mongoose-findorcreate to create the findOrCreate function needed for passportJS.

I am able to redirect to the facebook login form, and login with my facebook credentials. But afterwards I get redirected to the failureRedirect URL which return the following:


Cannot GET
/auth/facebook/callback?code=AQBsq2xY-YTsNMmipM45BVBBC9W1QovO1grjC64nDe1psWuRZQwjGv8SMpXuou3s9SaVIIS0_3RqJn-SQ339wPD_TWjRPIpXpZZVHerTRZEQnQYLvt1CM0qMzG5R_JNqfYib6rkmuP4epXou5HoqUa01qszUpYfGMPHUYyNVx1VdMSu3RLbdta8rYle9am539NzYL8ihlwlflnFku-8H3QqlzWJ5Ta1pQ29N0ZyRRu38nKzrDQk-jE5Sq9WECDZ6aUX-KWvexM_5UAUJ60crUPFVxZyWwV__5N6DyDEpS9SrWKdcrpdwcMOd4u0z6mbK7os


This is my model:

const mongoose = require('mongoose');
const findOrCreate = require('mongoose-findorcreate');

// Schema definition
const userSchema = mongoose.Schema({
provider: String,
id: String,
displayName: String,
name: [
{familyName: String},
{givenName: String},
{middleName: String},
],
emails: [
{value: String},
{type: String},
],
photos: [
{value: String},
],
});

userSchema.plugin(findOrCreate);

// Compile model from schema
module.exports = mongoose.model('User', userSchema);


The passport part of the app:

passport.use(new FacebookStrategy({
clientID: conf.facebookClientID,
clientSecret: conf.facebookSecret,
callbackURL: 'http://localhost:3000/auth/facebook/callback',
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate(function(err, user) {
if (err) { return done(err); }
done(null, user);
});
}
));

// Authentication routes
app.get('/auth/facebook', passport.authenticate('facebook'));

app.get('auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect: 'http://localhost:3000/words',
failureRedirect: 'http://localhost:3000/auth/facebook',
}));


I am trying to figure out why the login fails, and why the user does not get written into my database (I can query the database for other stuff so I know the connection is valid).

If you need more information about the project please ask and I will edit the question. You can also check the gitHub repo.

Answer

Alter:

app.get('auth/facebook/callback'

to:

app.get('/auth/facebook/callback'
Comments