使用Promise而不是自定义回调的passport.authenticate() [英] passport.authenticate() using a Promise instead of a Custom Callback
问题描述
passport.authenticate()
,如何定义 Promise 而不是使用 Custom Ballback ?
passport.authenticate()
, how can I define a Promise instead of using a Custom Ballback?
如何使用 passport.authenticate()
在此处引用: http://www.passportjs.org/docs/authenticate/
How to used passport.authenticate()
is referenced within here:
http://www.passportjs.org/docs/authenticate/
在此页面中,有一个部分自定义Ballback :
Within this page, there is a section Custom Ballback:
如果内置选项不足以处理身份验证请求,则可以提供自定义回调以允许应用程序处理成功或失败.
If the built-in options are not sufficient for handling an authentication request, a custom callback can be provided to allow the application to handle success or failure.
app.get('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/users/' + user.username);
});
})(req, res, next);
});
自定义回调定义为:
function(err, user, info){...}
我想做的是将这个自定义回调替换为承诺.
What I wish to do is replace this Custom Callback with a Promise.
[Promise](resolve, reject)
.then(res => {
})
.catch(err => {
})
我该怎么做?谢谢.
推荐答案
感谢大家对@ sterling-archer和@ el-finito的有用答复
Thanks all for your helpful responses @sterling-archer and @el-finito
我发现Passport.js Github存储库中的一个相关问题有助于使用Passport处理password.authenticate()回调:使用带有护照的节点来实现"
I had found a related issue within Passport.js Github repository helpful for using Passport to handle passport.authenticate() callback: "Using node's promisify with passport"
export const authenticate = (req, res) =>
new Promise((resolve, reject) => {
passport.authenticate(
[passport strategy],
{ session: false },
(err, user) => {
if (err) reject(new Error(err))
else if (!user) reject(new Error('Not authenticated'))
resolve(user)
})(req, res)
})
这篇关于使用Promise而不是自定义回调的passport.authenticate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!