router.post('/', async (ctx) => {
await passport.authenticate('local', (err, user) => {
if(user === false){
console.log(err);
ctx.render('/', {title: 'Авторизация', message: ctx.body});
} else {
const payload = {
id: user.id,
displayName: user.displayName,
email: user.email
};
const token = jwt.sign(payload, jwtsecret); //здесь создается JWT
ctx.body = {user: user.displayName, token: 'JWT ' + token};
ctx.redirect('/user');
}
}) (ctx)
});
(node:6516) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Can't set headers after they are sent. (node:6516) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js p rocess with a non-zero exit code.
router.post('/', async (ctx) => {
await passport.authenticate('local', async(err, user) => {
if(user === false){
await ctx.render('login', {title: 'Авторизация', message: 'Неверный логин или пароль'});
} else {
console.log(user);
const payload = {
id: user.id,
username: user.username,
};
const token = jwt.sign(payload, jwtsecret); //здесь создается JWT
ctx.body = {user: user.username, token: 'JWT ' + token};
console.log(ctx.body);
ctx.redirect('/user');
}
}) (ctx)
});
router.post('/', async (ctx) => {
const user = await new Promise((resolve, reject) => {
passport.authenticate('local', (err, user) => {
if (err) return reject(err);
return resolve(user);
})
})
if (!user) {
ctx.render('/', { title: 'Авторизация', message: ctx.body });
} else {
const payload = {
id: user.id,
displayName: user.displayName,
email: user.email
};
const token = jwt.sign(payload, jwtsecret); //здесь создается JWT
ctx.body = { user: user.displayName, token: 'JWT ' + token };
ctx.redirect('/user');
}
});