我正在尝试通过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/