首页 > 解决方案 > PassportJS 没有在本地重定向

问题描述

快速总结我非常奇怪的情况。我正在使用 PassportJS 的本地策略进行注册。它随机工作,然后停止工作。我会不时发布我护照的代码,然后在下面我将详细介绍。

首先是我的策略:

passport.use('register', new TwoFAStartegy({
  usernameField: 'email',
  passwordField: 'password',
  passReqToCallback: true,
  skipTotpVerification: true
}, async (req, email, password, done) => {
  if (password.length < 8) {
    done(null, false, req.flash('error', 'Password is not longer than 8 characters'));
  }
  if (password !== req.body.password_repeat) {
    done(null, false, req.flash('error', 'Passwords do not match'));
  }

  var isValidPass = await helpers.checkPassword(password)
  if (!isValidPass)
    done(null, false, req.flash('error', 'Password is not strong enough.'));

  const newUser = {
    email,
    password,
    first_name: req.body.fName,
    last_name: req.body.lName,
    practice: req.body.practice,
    role: 0
  }

  const duplicateUser = await pool.query('SELECT email FROM Users WHERE email = ?', newUser.email);
  if (duplicateUser != 0) {
    done(null, false, req.flash('error', 'Email Already Exists'));
  } else {
    newUser.password = await helpers.encryptPassword(password)

    const result = await pool.query('INSERT INTO Users SET ?', [newUser]);
    newUser.id = result.insertId;
    return done(null, newUser);
  }
}),
);

从这里调用:

router.post('/signup', isNotLoggedIn, passport.authenticate('register', {
  successRedirect: '/setup-2fa',
  failureRedirect: '/signup',
  failureFlash: true
}))

这是我尝试注册时的控制台输出

POST /signup 302 110.044 ms - 64
GET /setup-2fa 302 0.734 ms - 58
GET /signin 304 2.127 ms - -    
GET /signup 200 1.698 ms - 4923

此外,这是我在应用程序中设置会话的方式:

app.use(session({
  cookie: {maxAge: 300000},
  rolling: true,
  httpOnly: true,
  secure: true,
  secret: process.env.MYSECRET,
  resave: false,
  saveUninitialized: false,
  store: MySQLStore(database)
}));

现在代码已经发布了,我将更详细地介绍。每当我尝试注册时,数据都会按预期添加到数据库中。注册后,我检查了我的 cookie,发现 cookie 已被添加。但它不会在成功重定向后将我重定向到 /setup-2fa。它尝试如上所示,但它不这样做。它在某一时刻发生了并且没有任何改变(我浏览了提交历史,并且有一段时间没有关于护照的任何改变)。我在我的服务器和本地都设置了我的所有 SSL 和东西,并且都有锁定图标,所以我怀疑是这样,我怀疑是这样,因为我能够早点注册就好了。如果我刷新我的页面,我可以作为普通用户继续。任何关于从哪里开始甚至查看的提示将不胜感激。

Bad Gateway
The proxy server received an invalid response from an upstream server.
Additionally, a 302 Found error was encountered while trying to use an ErrorDocument to handle the request.

标签: node.jsexpresspassport.jspassport-local

解决方案


推荐阅读