在单独的函数中调用 passport.authenticate 无效
Calling passport.authenticate in a separate function isn't working
我对 Node 和 Passport 比较陌生,我正在尝试使用 OAuth 登录表单。我在设置 Passport 时没有遇到任何问题,它功能齐全。
它只是在我开始清理代码以将路由与中间件分开时才崩溃。
我已经包含了更改前后的部分代码。
所以,这有效:
module.exports = function(app, passport) {
app.get('/login', loginIndex)
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/profile',
failureRedirect : '/login',
failureFlash : true
}))
function loginIndex(req, res) {
res.render('login.ejs', {message: req.flash('loginMessage')})
}
}
但这不是:
module.exports = function(app, passport) {
app.get('/login', loginIndex)
app.post('/login', loginAuth)
function loginIndex(req, res) {
res.render('login.ejs', {message: req.flash('loginMessage')});
}
function loginAuth(){
passport.authenticate('local-login', {
successRedirect : '/profile',
failureRedirect : '/login',
failureFlash : true
})
}
}
因此,两者之间的唯一区别是我将 passport.authenticate() 调用移到了函数 loginAuth() 中。
我想这与 passport.authenticate() 的内部工作有关,但我不确定。
谢谢。
试试这个:
app.post('/login', loginAuth())
...
function loginAuth(){
return passport.authenticate('local-login', {
successRedirect : '/profile',
failureRedirect : '/login',
failureFlash : true
})
}
在您的原始代码中,您正在执行 passport.authenticate
,而在第二个版本中,您只是传递一个函数而不执行通行证逻辑。
我对 Node 和 Passport 比较陌生,我正在尝试使用 OAuth 登录表单。我在设置 Passport 时没有遇到任何问题,它功能齐全。
它只是在我开始清理代码以将路由与中间件分开时才崩溃。
我已经包含了更改前后的部分代码。
所以,这有效:
module.exports = function(app, passport) {
app.get('/login', loginIndex)
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/profile',
failureRedirect : '/login',
failureFlash : true
}))
function loginIndex(req, res) {
res.render('login.ejs', {message: req.flash('loginMessage')})
}
}
但这不是:
module.exports = function(app, passport) {
app.get('/login', loginIndex)
app.post('/login', loginAuth)
function loginIndex(req, res) {
res.render('login.ejs', {message: req.flash('loginMessage')});
}
function loginAuth(){
passport.authenticate('local-login', {
successRedirect : '/profile',
failureRedirect : '/login',
failureFlash : true
})
}
}
因此,两者之间的唯一区别是我将 passport.authenticate() 调用移到了函数 loginAuth() 中。 我想这与 passport.authenticate() 的内部工作有关,但我不确定。
谢谢。
试试这个:
app.post('/login', loginAuth())
...
function loginAuth(){
return passport.authenticate('local-login', {
successRedirect : '/profile',
failureRedirect : '/login',
failureFlash : true
})
}
在您的原始代码中,您正在执行 passport.authenticate
,而在第二个版本中,您只是传递一个函数而不执行通行证逻辑。