我正在做一个简单的注册应用程序使用Koa,koa-passport,passport-local和koa-joi-router,这只是用来验证body json。示例建议像这样使用passport.authenticate:
router.route({
method: 'post',
path: '/signup',
validate: {
type: 'json',
body: {
username: Joi.string().required(),
password: Joi.string().required()
}
},
async handler(ctx, next) {
const body: {
username: string,
password: string,
} = ctx.request.body;
try {
await User.createAndSave(body.username, body.password);
return passport.authenticate('local', async (error, user, info, status) => {
if(error) {
ctx.throw(500);
} else if(user) {
await ctx.login(user);
} else {
ctx.throw(400);
}
})(ctx, next);
} catch {
ctx.throw(500);
}
}
}正如您所看到的,我首先将用户输入到我的数据库中,然后调用passport.authenticate,但是奇怪的是,没有将用户的用户名或密码传递给它。那么,该函数如何知道我希望它找到哪个用户呢?
发布于 2019-09-20 02:57:03
经过一些搜索,我发现用户名和密码主体字段名都设置在passport-local包中。Reference
https://stackoverflow.com/questions/58016944
复制相似问题