Я пытаюсь реализовать локальную аутентификацию с помощью паспорта js и sequalize в экспрессе.Проблема, с которой я столкнулся, заключается в том, что обратный вызов успеха никогда не срабатывает при успешной аутентификации.Журнал показывает, что пользователь найден, но объект users никогда не регистрируется в обратном вызове запроса и не вызывается res.send.
маршрут и стратегия
passport.use(
'local',
new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true,
},
(req, email, password, done) => {
// find user
User.findOne({ where: { email } }).then(user => {
if (!user) {
return done(null, false);
}
if (password !== user.password) {
return done(null, false);
}
console.log('user found!');
return done(null, user);
});
},
),
);
app.use(passport.initialize());
// routes
app.post(
'/login',
passport.authenticate('local'),
(req, res) => {
console.log(req.user);
res.send('logged in');
// const expiresIn = 60 * 60 * 24 * 180; // 180 days
// const token = jwt.sign(req.user, config.auth.jwt.secret, { expiresIn });
// res.cookie('id_token', token, { maxAge: 1000 * expiresIn, httpOnly: true });
},
);
журналы приложений
express:router:layer new '/' +0ms
express:router:route new '/login' +1ms
express:router:layer new '/login' +0ms
express:router:route post '/login' +0ms
express:router:layer new '/' +0ms
express:router:route post '/login' +0ms
express:router:layer new '/' +0ms
express:router:route new '/users' +0ms
express:router:layer new '/users' +0ms
express:router:route get '/users' +0ms
express:router:layer new '/' +0ms
express:router:route new '/signup' +0ms
express:router:layer new '/signup' +1ms
express:router:route post '/signup' +0ms
express:router:layer new '/' +0ms
express:router use '/' <anonymous> +0ms
express:router:layer new '/' +0ms
express:router use '/' <anonymous> +0ms
express:router:layer new '/' +0ms
express:router use '/' <anonymous> +0ms
express:router:layer new '/' +0ms
express:application set "port" to '3001' +0ms
Executing (default): SELECT 1+1 AS result
Connection has been established successfully.
express:router dispatching POST /login +4s
express:router query : /login +2ms
express:router expressInit : /login +0ms
express:router favicon : /login +2ms
express:router logger : /login +0ms
express:router jsonParser : /login +2ms
express:router urlencodedParser : /login +28ms
express:router cookieParser : /login +0ms
express:router serveStatic : /login +1ms
express:router initialize : /login +0ms
Executing (default): SELECT "id", "firstName", "lastName", "username", "email", "password", "createdAt", "updatedAt" FROM "users" AS "user" WHERE "user"."email" = 'a' LIMIT 1;
user found!
express:router <anonymous> : /login +25ms
express:router <anonymous> : /login +1ms
express:view require "jade" +0ms
express:view lookup "error.jade" +190ms
express:view stat "/home/zmigaddo/sc/views/error.jade" +0ms
express:view render "/home/zmigaddo/sc/views/error.jade" +1ms
POST /login 500 346.104 ms - 1910