【问题标题】:Facebook passport auth, exporting passport.authenticate methodFacebook 护照认证,导出 passport.authenticate 方法
【发布时间】:2018-03-01 11:00:45
【问题描述】:

我正在尝试通过 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 护照身份验证也能按预期工作我怎样才能让它工作并将 passport.authenticate 保存在单独的文件中?

【问题讨论】:

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


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 2019-01-06
      • 2015-09-23
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多