gpt4 book ai didi

通过 Apache-Redirect 代理进行 Node.js Passport 身份验证

转载 作者:太空宇宙 更新时间:2023-11-03 22:34:37 25 4
gpt4 key购买 nike

我正在构建一个带有集成 Passport 帐户系统的 Node.js 应用程序。由于我托管在 uberspace.de 上,因此我需要在主 Web 根目录中配置我的 .htaccess,如下所示:

RewriteEngine On
RewriteRule ^(.*) http://localhost:34457/$1 [P]

我的快速登录路线是:(可在 /api/auth/login 访问)

router.post('/login', passport.authenticate('login', { 
successRedirect: '/account',
failureRedirect: '/login?error=true'
}));

按照我对 Passport 的理解,如果登录成功,我应该被重定向到 /account,如果没有,则应该重定向到 /login?error=true

但是如果我使用

执行 POST
url --data "email=foo@bar.com&password=test" http://[domain]/api/auth/login

结果是:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>502 Proxy Error</title>
</head><body>
<h1>Proxy Error</h1>
<p>The proxy server received an invalid
response from an upstream server.<br />
The proxy server could not handle the request <em><a href="/api/auth/login">POST&nbsp;/api/auth/login</a></em>.<p>
Reason: <strong>Error reading from remote server</strong></p></p>
<hr>
<address>Apache/2.2.15 (CentOS) Server at [domain] Port 80</address>
</body></html>

如果我在 Chrome 中通过 html 表单(方法:POST,操作:/api/auth/login)执行相同的查询,我会被重定向到 /api/auth/login%5E (显然返回 404)。

像这样的简单重定向可以工作:

router.post('/redirectToHome', function(req, res, next) {
res.redirect(302, '/');
});

但是即使我在调用/api/auth/login时执行这个函数

router.post('/login', function(req, res, next) {
passport.authenticate('login', function(err, user, info) {
if (err) return next(err);
if (!user) {
console.log(info);
return res.json(401, {success: false});
} else {
console.log(info);
return res.json(200, {success: true});
}
})(req, res, next);
});

我仍然会被重定向到 /api/auth/login%5E

我的登录身份验证策略实现为:

var LocalStrategy   = require('passport-local').Strategy;
var User = require('../models/user');
var bCrypt = require('bcrypt-nodejs');

module.exports = function(passport){

passport.use('login', new LocalStrategy({
usernameField: 'email',
passReqToCallback : true
}, function(req, email, password, done) {
// check in mongo if a user with username exists or not
User.findOne({ 'email' : email },
function(err, user) {
// In case of any error, return using the done method
if (err)
return done(err);
// Username does not exist, log the error and redirect back
if (!user){
console.log('User Not Found with email '+email);
return done(null, false, req.flash('message', 'User Not found.'));
}
// User exists but wrong password, log the error
if (!isValidPassword(user, password)){
console.log('Invalid Password');
return done(null, false, req.flash('message', 'Invalid Password')); // redirect back to login page
}
// User and password both match, return user from done method
// which will be treated like success
return done(null, user);
});
}));
var isValidPassword = function(user, password){
return bCrypt.compareSync(password, user.password);
}
}

即使login-router写成如下:

router.post('/login', function(req, res, next) {
passport.authenticate('login', function(err, user, info) {
if (err) return next(err);
if (!user) {
console.log(info);
return res.json(401, {success: false});
} else {
console.log(info);
return res.json(200, {success: true});
}
})(req, res, next);
});

我仍然被重定向到 /api/auth/login%5E

我的 Passport 登录策略是这样实现的:

var LocalStrategy   = require('passport-local').Strategy;
var User = require('../models/user');
var bCrypt = require('bcrypt-nodejs');

module.exports = function(passport){

passport.use('login', new LocalStrategy({
usernameField: 'email',
passReqToCallback : true
}, function(req, email, password, done) {
// check in mongo if a user with username exists or not
User.findOne({ 'email' : email },
function(err, user) {
// In case of any error, return using the done method
if (err)
return done(err);
// Username does not exist, log the error and redirect back
if (!user){
console.log('User Not Found with email '+email);
return done(null, false, req.flash('message', 'User Not found.'));
}
// User exists but wrong password, log the error
if (!isValidPassword(user, password)){
console.log('Invalid Password');
return done(null, false, req.flash('message', 'Invalid Password')); // redirect back to login page
}
// User and password both match, return user from done method
// which will be treated like success
return done(null, user);
});
}));
var isValidPassword = function(user, password){
return bCrypt.compareSync(password, user.password);
}
}

问题是什么?

最佳答案

实际上,我的问题是一些不可见的字符,它将我重定向到其他页面,然后导致了许多其他问题。

关于通过 Apache-Redirect 代理进行 Node.js Passport 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31416095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com