I am making a simple signup app using Koa, koa-passport, passport-local and koa-joi-router which is only used to validate body json. Examples suggest the use of passport.authenticate
like this:
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);
}
}
}
As you can see I first enter the user into my database and then call passport.authenticate
but weirdly, do not pass the username or password of the user to it. So how is that function supposed to know which user I want it to find?