来描述我的问题:有一个反应页面,用户填写电子邮件,..并使用 Axios.post 将其发送到快速 BE - 该部分有效。但是我希望用户在错误登录后被重定向到注册页面(/registration)。我使用 res.redirect("http://localhost:3000/registration") 但它一遍又一遍地抛出相同的错误。
对预检请求的响应未通过访问控制检查:“Access-Control-Allow-Origin”标头的值“http://localhost:3000”不等于提供的来源。
//Authenticate user
router.post("/auth", async (req, res) => {
const pass = req.body.pass
const email = req.body.email
//Same as SELECT command
await User.findOne({
where: {
email: email
}
}).then(function (user) {
if(!user) {
res.redirect("http://localhost:3000/registration")
} else {
bcrypt.compare(pass, user.pass, function (err, result) {
if (result == true) {
console.log("Logged in")
res.send({
token: "123123"
})
} else {
res.redirect('/')
}
})
}
})
})
const express = require('express')
const cors = require('cors')
const app = express()
const db = require('./models/index')
app.use(express.urlencoded({extended: true}))
app.use(express.json())
app.use(cors())
db.sequelize.sync({ force: true }).then(() => {
console.log("Drop and re-sync db.");
});
app.get("/", (req, res) => {
//res.json({message: "WELCOME"})
res.redirect("http://localhost:3000/")
})
require("./routes/user")(app)
require("./routes/category")(app)
require("./routes/product")(app)
app.listen(3001, () => {
console.log("running on port 3001")
})
答案 0 :(得分:0)
当登录失败时,在 React 中基于类的组件中使用 this.props.history.push('/registration')
重定向。
编辑: 如果您使用的是基于函数的组件,那么
import { useHistory } from "react-router-dom";
let history = useHistory();
使用 history.push('/registration')
重定向
答案 1 :(得分:0)
找到了!
window.location.replace("some location")
或
window.location = "some location"