Passportjs 成功回调从未被调用

Posted

技术标签:

【中文标题】Passportjs 成功回调从未被调用【英文标题】:Passportjs success callback never called 【发布时间】:2019-06-14 10:56:34 【问题描述】:

我正在尝试使用护照 js 实现本地身份验证并在 express 中进行均衡。我遇到的问题是成功回调永远不会在成功的身份验证时触发。日志显示找到了用户,但用户对象从未记录在请求回调中,也没有触发 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

【问题讨论】:

我没有看到任何LocalStrategy接收req作为参数的例子,总是只有3个参数,像这样:(用户名,密码,完成)试着像这个网站的例子那样做:passportjs.org/docs/username-password DavidVicente 是的,我也尝试删除 req 参数,但没有效果。 【参考方案1】:

我解决了这个问题。再次运行后,我收到错误Error: Failed to serialize user into session。在this question 之后,我添加了以下代码来序列化和反序列化用户并解决问题。

passport.serializeUser(function(user, done) 
  done(null, user);
);

passport.deserializeUser(function(user, done) 
  done(null, user);
);

【讨论】:

以上是关于Passportjs 成功回调从未被调用的主要内容,如果未能解决你的问题,请参考以下文章

PhoneGap 插件:从未调用过 AudioEncode 成功回调

Social.localUser.Authenticate 回调从未被调用

iOS AWS Appsync Fetch 回调从未被调用

猫鼬保存卡住(从未调用过回调)

Kivy Android plyer.gps on_location 回调从未被调用

使用 PassportJS 响应本机社交登录