javascript - Facebook Passport Auth,导出 Passport.authenticate 方法

标签 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)

  }
))

The question is

为什么当我写作时

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

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

然后就可以了,为什么呢?即使使用相同的文件夹结构,我的 JWT Passport 身份验证也能按预期正常工作,我怎样才能使其工作并将 Passport.authenticate 保存在单独的文件中?

最佳答案

我不确定..但我的可以用这个! 不妨只划分 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);

关于javascript - Facebook Passport Auth,导出 Passport.authenticate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48662688/

相关文章:

javascript - 当导航栏固定时添加边距

ios - Facebook api swift 当前用户为零

ios - 有没有一个很好的教程来使用 Storyboard实现 facebook 登录到 ios 应用程序?

javascript - 代码整合与复杂性

javascript - Firebug 显示构造函数的无限递归

javascript - 使用 D3.js 和 TUIO 的触摸事件

ios - 使用 Facebook 的 FirebaseSimpleLogin 后,Firebase observeEventType 没有触发?

node.js - nodeJS - 如何使用 express 创建和读取 session

javascript - 从单个上 Node 连接的两端公开的远程接口(interface)

node.js - Nodejs XMPP 组件使用