Axios + React JS 护照 JS 登录重定向不起作用?
Axios + reactJS+ passportJS login redirect not working?
我是第一次尝试实现用户身份验证,成功登录后我无法使重定向工作。
反应函数:
const login = () => {
Axios({
method: "POST",
data: {
email: loginEmail,
username: loginEmail,
password: loginPassword,
},
withCredentials: true,
url: "http://localhost:5000/login",
}).then((res) => console.log(res));
};
后端用户控制器:
exports.login = (req, res) => {
const { email, name, username, password } = req.body;
const user = new User({ email, name, username });
req.login(user, (err) => {
if (err) return next(e);
console.log("LOGGED IN")
res.redirect('/');
});
};
当我输入有效的 user/password 时,控制台消息会打印出来,但我无法在成功登录后让页面重定向。我尝试使用 history.push 而不是后台跳转,但无论是否登录成功都会推送
const login = () => {
Axios({
method: "POST",
data: {
email: loginEmail,
username: loginEmail,
password: loginPassword,
},
withCredentials: true,
url: "http://localhost:5000/login",
}).then(history.push("/"));
};
有人知道为什么我无法从后端重定向吗?我怎样才能更好地解决这个问题?
试试这个..
const login = async () => {
const res = await Axios({
method: "POST",
data: {
email: loginEmail,
username: loginEmail,
password: loginPassword,
},
withCredentials: true,
url: "http://localhost:5000/login",
});
// if success
history.push("/");
};
在后端用户控制器而不是 res.redirect
中发送 res.send(// if authentication is completed send true, else send false)
。
现在您可以在 then
函数中访问此消息。
const login = () => {
Axios({
method: "POST",
data: {
email: loginEmail,
username: loginEmail,
password: loginPassword,
},
withCredentials: true,
url: "http://localhost:5000/login",
}).then((res) => {(res.data===true) && (history.push("/"))});
};
我是第一次尝试实现用户身份验证,成功登录后我无法使重定向工作。
反应函数:
const login = () => {
Axios({
method: "POST",
data: {
email: loginEmail,
username: loginEmail,
password: loginPassword,
},
withCredentials: true,
url: "http://localhost:5000/login",
}).then((res) => console.log(res));
};
后端用户控制器:
exports.login = (req, res) => {
const { email, name, username, password } = req.body;
const user = new User({ email, name, username });
req.login(user, (err) => {
if (err) return next(e);
console.log("LOGGED IN")
res.redirect('/');
});
};
当我输入有效的 user/password 时,控制台消息会打印出来,但我无法在成功登录后让页面重定向。我尝试使用 history.push 而不是后台跳转,但无论是否登录成功都会推送
const login = () => {
Axios({
method: "POST",
data: {
email: loginEmail,
username: loginEmail,
password: loginPassword,
},
withCredentials: true,
url: "http://localhost:5000/login",
}).then(history.push("/"));
};
有人知道为什么我无法从后端重定向吗?我怎样才能更好地解决这个问题?
试试这个..
const login = async () => {
const res = await Axios({
method: "POST",
data: {
email: loginEmail,
username: loginEmail,
password: loginPassword,
},
withCredentials: true,
url: "http://localhost:5000/login",
});
// if success
history.push("/");
};
在后端用户控制器而不是 res.redirect
中发送 res.send(// if authentication is completed send true, else send false)
。
现在您可以在 then
函数中访问此消息。
const login = () => {
Axios({
method: "POST",
data: {
email: loginEmail,
username: loginEmail,
password: loginPassword,
},
withCredentials: true,
url: "http://localhost:5000/login",
}).then((res) => {(res.data===true) && (history.push("/"))});
};