node.js - 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.
解决方案
推荐阅读
- swift - 从函数返回符合 CaseIterable 的通用类型
- bash - 每个日期创建一个文件,仅包含与该日期对应的文件名
- c# - 无法使用 WebBrowser 控件等待网页加载
- shell - symfony4:启动和跟踪长后台任务的进度
- javascript - 打字机 JS,如何删除 Fast 中的字符,按原样在 Normal 中键入字符
- performance-testing - JMETER 4. 0 | JMeter 分布式负载测试 | jp@gc - 步进线程组
- elasticsearch - Elasticsearch 部分匹配顺序
- angular - 基于 Angular 6 角色的根 url 路由
- c# - C#:是否可以将位图保存为 GIF 和 JPEG 而不会进行有损压缩?
- html - 在 VS Code 中粘贴时获得正确的缩进