passport-facebook
passport-facebook copied to clipboard
Redirect loop issue after facebook callback.
Hi @jaredhanson , thanks a ton for working on this code. I've been looking forward to using passport & passport-facebook with a Sails.js app but I've run into an odd issue I was hoping to get some help with:
When hitting /auth/facebook
, getting redirecting to facebook to login, and then redirecting back to my dummy sails.js app I run into a redirect loop. I've looked at #44, thinking it may be related but the issue persists. Sandbox mode is off, and the implementation in Sails.js (express) is very basic:
express middleware
module.exports = express:
customMiddleware: (app) ->
passport.use new FBStrategy(
clientID: ""
clientSecret: ""
callbackURL: "http://localhost:1337/auth/facebook/callback"
, (accessToken, refreshToken, profile, done) ->
console.log accessToken, refreshToken, profile
User.findOne(or: [
facebook_id: profile.id
,
facebook_id: profile.id
]).done (err, user) ->
if user
console.log user
done null, user
else
return console.log(profile)
done null, user
)
app.use passport.initialize()
app.use passport.session()
/auth/facebook/
& /auth/facebook/callback/
routes
facebook: (req, res)->
passport.authenticate("facebook"
, (err, user) ->
req.logIn user, (err) ->
console.log user
if err
console.log err
res.view "500"
return
res.redirect "/"
return
)(req, res)
facebook_callback: (req, res)->
# Redirect loop happens here as soon as passport.authenticate is called again
passport.authenticate('facebook',
{ failureRedirect: '/login' },
(req, res)->
console.log req.query
res.redirect('/');
)(res ,res)
Facing same issue.
Facing same issue, could you find any way?
@flarocca I stopped coding in nodejs, that solved it.
I face the same issue also.
Try this:
//1
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy
//2- Configure Express
server.use(passport.initialize());
//3- COnfigure passport
passport.serializeUser((player, done) => {
done(null, player);
});
//4- Setup a Strategy
passport.use('facebook', new FacebookStrategy({
clientID: config.facebook.appId,
clientSecret: config.facebook.appSecret,
callbackURL: config.facebook.callback,
profileFields: ['id', 'birthday', 'displayName', 'picture.type(large)', 'email'],
passReqToCallback: true
}, (req, token, refreshToken, profile, done) => {
//Do your staff here, whatever you want...
//But this is very very important
return done(null, profile);
}));
//5- Add route for authenticating
server.get('/auth/facebook', passport.authenticate('facebook', { session: false, scope: ['public_profile', 'user_birthday', 'email'] }));
//6- Add route as a callback, facebook will redirect here after user logged in
server.get('/auth/facebook/callback', passport.authenticate('facebook', { session: false }), (req, res) => {
res.json(200, "you should be able to be here");
});
It's very important that you have already configured your application in facebook developer
You could probably use https://github.com/niftylettuce/express-redirect-loop to fix this issue.
flarocca , you solved the issue , thanks!