Stas
Stas

Reputation: 695

Facebook passport auth, exporting passport.authenticate method

I'm trying to authenticate user through passport-facebook, and there is 1 really odd thing happening which im really unsure of. When defining my routes i have this line of code

app.get('/login/facebook',
        IsAuth.facebookEnter ),

//passport.authenticate('facebook')

    app.get('/login/facebook/return', 
        IsAuth.facebookComeBack)

And in my IsAuth file i have the following

const passport = require('passport')


const FacebookStrategy = require('../passport/facebook_passport')


module.exports = {


    facebookEnter(){
       passport.authenticate('facebook')
    },
    facebookComeBack(){
        passport.authenticate('facebook', { failureRedirect: '/login' }),
            function(req, res) {
                res.redirect('/');
            }
    }


}
and the strategy itself here

const passport = require('passport')
const User = require('../models/User')
const FacebookStrategy = require('passport-facebook')

passport.use(new FacebookStrategy({
    clientID: "ALMOST POSTED IT",
    clientSecret: "Ntest test",
    //call baack to one of our routes to valiate the auth request
    //is called when the user pressed on OK to login with facebook
    callbackURL: "http://localhost:8888/login/facebook/return"
  },
  function(accessToken, refreshToken, profile, cb) {
        console.log(profile, accessToken, refreshToken)

  }
))

The question is

Why when i write

app.get('/login/facebook',
        IsAuth.facebookEnter ),
it doesnt work but if i write this code

app.get('/login/facebook',
            passport.authenticate('facebook') ),

then it works, so why? My JWT passport authnetication works good as expected even though using the same folder structure How can i make it work and keep passport.authenticate in a seperate file?

Upvotes: 0

Views: 27

Answers (1)

Tetsuya3850
Tetsuya3850

Reputation: 1019

I'm not sure.. but mine works with this! Might as well just divide the module.exports?

module.exports.fbAuth = passport.authenticate("facebook");

module.exports.fbAuthCB = function(req, res, next) {
  passport.authenticate("facebook", (err, user, info) =>
    generateTokenAndRedirect(err, user, info, req, res, next)
  )(req, res);
};

router.get("/auth/facebook", authCtrl.fbAuth);
router.get("/auth/facebook/callback", authCtrl.fbAuthCB);

Upvotes: 1

Related Questions