我有一个从Axios请求中查询的快递邮政路线。其目标是仅对用户进行身份验证并将其重定向到受保护的页面。身份验证成功,但重定向不会呈现新视图。为什么res.redirect
不起作用,如何获得重定向以呈现新的React页面?
我有一个相当独特的开发环境,试图复制生产版本:
我通过调用res.redirect(30_, '/myhome')
尝试了每种重定向类型(例如301、302和307),其中_是每个请求中的最后一个数字。
我推测Firefox可能有一些默认行为,阻止自签名证书的重定向。我弄乱了一些浏览器设置,但没有起作用。我也已经在Safari中复制了此问题的价值。
axios.post('/login', {
username: values.username,
password: values.password,
}).catch(
err => {
this.setState({
error: {
message: err.response.data.message,
}
});
}
);
// Most route that is queried
router.post('/login', (req, res, next) => {
const {email, username, password} = req.body;
if((email || username) && password){
// Either username or password can be used to login
let creds = {
email: email,
password: password,
username: username
};
// Database (mongodb) handles authentication
User.authenticate(creds, (err, usr) => {
if (err) {
err = new Error(error.BAD_LOGIN);
err.level = 'warn';
err.status = status.UNAUTHORIZED;
return next(err); // Is passed on to error handler via next call (no error logged)
}else if(!usr){
let err = Error('Incorrect username or password.');
err.status = status.UNAUTHORIZED;
return next(err); // No error printed
}
req.session.userId = usr._id;
console.log('Success'); // Is printed to the console
return res.redirect('/myhome'); // The redirect that fails
});
return;
}
let err = new Error('Email and password fields required.');
err.status = status.BAD_REQUEST;
next(err); // No error logged
});
{
"name": "website-front-end",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "jest",
"dev": "nodemon server.js -vvvvv",
"start": "nodemon server.js",
"lint": "eslint ./ --fix",
"purgelogs": "rm -rf ./logs"
},
"keywords": [],
"author": "",
"license": "",
"dependencies": {
"@zeit/next-less": "^1.0.1",
"@zeit/next-sass": "^1.0.1",
"antd": "^3.17.0",
"aos": "^2.3.4",
"axios": "^0.19.0",
"bcrypt": "^3.0.6",
"connect-mongo": "^3.0.0",
"css-math": "^0.4.0",
"email-templates": "^6.0.0",
"express": "^4.17.1",
"express-session": "^1.16.2",
"http-status": "^1.3.2",
"less": "^2.7.3",
"less-vars-to-js": "^1.3.0",
"lorem-ipsum": "^2.0.1",
"mongoose": "^5.6.2",
"next": "^8.0.4",
"next-compose": "0.0.2",
"node-sass": "^4.11.0",
"nodemailer": "^6.3.0",
"password-validator": "^4.1.1",
"prop-types": "^15.7.2",
"qs": "^6.7.0",
"querystring": "^0.2.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-youtube": "^7.9.0",
"scss": "^0.2.4",
"units-css": "^0.4.0",
"urlencode": "^1.1.0",
"uuid-mongodb": "^2.1.1",
"winston": "^3.2.1",
"yargs": "^13.3.0"
},
"devDependencies": {
"@babel/core": "^7.4.3",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.7.1",
"babel-plugin-import": "^1.12.0",
"babel-plugin-inline-react-svg": "^1.1.0",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.12.1",
"eslint": "^5.16.0",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.12.4",
"identity-obj-proxy": "^3.0.0",
"jest": "^24.7.1",
"react-addons-test-utils": "^15.6.2",
"react-svg-loader": "^2.1.0",
"webpack-cli": "^3.3.6"
}
}
Host: www.website.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Firefox/68.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://www.website.com/signin
Content-Type: application/json;charset=utf-8
Content-Length: 49
DNT: 1
Connection: keep-alive
发布请求返回状态代码302。
HTTP/1.1 302 Found
X-Powered-By: Express
Location: /myhome
Vary: Accept
Content-Type: text/plain; charset=utf-8
Content-Length: 30
Set-Cookie: connect.sid=s%3AHem_Jo7jDFZ0xFz4_saY_da7USFz3VCm.9Uvt%2FW6%2BZ5Krvodm9uRoO7QdBt1snQN6vwBO5rZ3s5U; Path=/; HttpOnly
Date: Mon, 12 Aug 2019 17:34:49 GMT
Connection: keep-alive
将获取请求重定向到受保护的路由到127.0.0.1:443:
Host: www.website.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Firefox/68.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: application/json;charset=utf-8
Referer: https://www.website.com/signin
DNT: 1
Connection: keep-alive
Cookie: connect.sid=s%3AHem_Jo7jDFZ0xFz4_saY_da7USFz3VCm.9Uvt%2FW6%2BZ5Krvodm9uRoO7QdBt1snQN6vwBO5rZ3s5U
HTTP/1.1 200 OK
X-Powered-By: Express
Cache-Control: no-store, must-revalidate
ETag: "75c-ZfHLepOsT6dLq3dgAF6hwjkWLN4"
Content-Type: text/html; charset=utf-8
Content-Length: 1884
Date: Mon, 12 Aug 2019 17:34:50 GMT
Connection: keep-alive
浏览器发出发布请求,后跟获取重定向。但是,该页面从不呈现。如何重定向到另一个页面?