Facebook护照身份验证,导出passport.authenticate方法

时间:2018-02-07 11:28:38

标签: javascript node.js facebook authentication passport.js

我试图通过passport-facebook对用户进行身份验证,并且发生了一件非常奇怪的事情,我真的不确定。在定义我的路线时,我有这行代码

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

//passport.authenticate('facebook')

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

在我的IsAuth文件中,我有以下

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)

  }
))
  

问题是

为什么我写

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

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

那么它有效,为什么呢?即使使用相同的文件夹结构,我的JWT护照authnetication仍然按预期工作良好如何使其工作并将passport.authenticate保存在单独的文件中?

1 个答案:

答案 0 :(得分:1)

我不确定..但我的工作原理! 可能只是划分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);