passport.authenticate() 使用 Promise 而不是自定义回调
passport.authenticate() using a Promise instead of a Custom Callback
passport.authenticate()
,如何定义 Promise 而不是使用 Custom Ballback?
passport.authenticate()
的使用方法参考如下:
http://www.passportjs.org/docs/authenticate/
在此页面中,有一个部分 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){...}
我想做的是用 Promise.
替换这个 Custom Callback
[Promise](resolve, reject)
.then(res => {
})
.catch(err => {
})
我该怎么做?谢谢。
您可以使用 es6-promisify 包。它非常容易使用,这里是一个例子:
const {promisify} = require("es6-promisify");
// Convert the stat function
const fs = require("fs");
const stat = promisify(fs.stat);
// Now usable as a promise!
stat("example.txt").then(function (stats) {
console.log("Got stats", stats);
}).catch(function (err) {
console.error("Yikes!", err);
});
感谢@sterling-archer 和@el-finito 的热心回复
我在 Passport.js Github 存储库中发现了一个相关问题,它有助于使用 Passport 处理 passport.authenticate() 回调:
"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)
})
passport.authenticate()
,如何定义 Promise 而不是使用 Custom Ballback?
passport.authenticate()
的使用方法参考如下:
http://www.passportjs.org/docs/authenticate/
在此页面中,有一个部分 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){...}
我想做的是用 Promise.
替换这个 Custom Callback[Promise](resolve, reject)
.then(res => {
})
.catch(err => {
})
我该怎么做?谢谢。
您可以使用 es6-promisify 包。它非常容易使用,这里是一个例子:
const {promisify} = require("es6-promisify");
// Convert the stat function
const fs = require("fs");
const stat = promisify(fs.stat);
// Now usable as a promise!
stat("example.txt").then(function (stats) {
console.log("Got stats", stats);
}).catch(function (err) {
console.error("Yikes!", err);
});
感谢@sterling-archer 和@el-finito 的热心回复
我在 Passport.js Github 存储库中发现了一个相关问题,它有助于使用 Passport 处理 passport.authenticate() 回调: "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)
})