如何将保存在护照中的用户从后端服务器发送到 React 前端服务器?
How can I send the user saved in passport from the backend server to the React frontend server?
我正在使用一个后端服务器来处理 React 中的前端服务器逻辑。我需要的是将登录的用户从后端服务器发送到前端服务器。但首先,我将解释用户登录后我的代码流如何以基本方式工作。
passport.use('local-signin', new LocalStrategy({
usernameField: 'celnumber',
passwordField: 'celnumber',
passReqToCallback: true
}, async (req, names, celnumber, done) => {
const user = await userController.findByCelNumber(celnumber);
if(!user) {
return done(null, false, localStorage.setItem('failureMessage', 'El número ingresado no existe.'));
}
localStorage.setItem('successMessage', 'Te has logueado satisfactoriamente.')
return done(null, user);
}));
登录成功后,从successRedirect
:
重定向到前端服务器
router.post("/login", passport.authenticate("local-signin",
{
successRedirect: "http://localhost:3001",
failureRedirect: "/users/login",
passReqToCallback: true
}
));
澄清:successRedirect:" http: // localhost: 3001 "
只是为了让护照不会给我任何错误,但我根本没有真正使用它。我正在通过如下所示的 window.location.href 进行重定向。
最后是我的自定义挂钩 useFormHelper
,我在其中重定向前端服务器以呈现初始页面。
if(type === "login") {
e.preventDefault();
await axios.post('http://localhost:3000/users/login', form).then(async function (response){
alert("Logueo completado satisfactoriamente.")
window.location.href = "http://localhost:3001/";
});
}
那么我的问题是,我如何将登录 passport 的用户从后端服务器本身发送到 React 前端服务器?
我在类似的 post 中看到我可以使用我的 windows.location.href 并将其传递给用户,例如:window.location.href =" http: // localhost: 3001 / "+ user
,但理论上它不是很安全。
我还想到通过 succesRedirect 发送用户,但我没有 'req' 来发送护照 req.user。 successRedirect:" http: // localhost: 3001 "+ req.user,
我希望我的解释是正确的,不要犹豫问我任何问题。谢谢。
在 router.post
中添加一个回调函数,您将在其中将 req 和 res 作为参数:
router.post("/login", passport.authenticate("local-signin",
{
successRedirect: "http://localhost:3001",
failureRedirect: "/users/login",
passReqToCallback: true
}
), (req, res)=>{
// If you use "Content-Type": "application/json"
// req.isAuthenticated is true if authentication was success else it is false
res.json({auth: req.isAuthenticated()});
});
在 then 函数的前端你可以有这样的参数:
axios.post(Your code).then((res)=>res.data.auth); // res.data will be the object that was sent from backend
我正在使用一个后端服务器来处理 React 中的前端服务器逻辑。我需要的是将登录的用户从后端服务器发送到前端服务器。但首先,我将解释用户登录后我的代码流如何以基本方式工作。
passport.use('local-signin', new LocalStrategy({
usernameField: 'celnumber',
passwordField: 'celnumber',
passReqToCallback: true
}, async (req, names, celnumber, done) => {
const user = await userController.findByCelNumber(celnumber);
if(!user) {
return done(null, false, localStorage.setItem('failureMessage', 'El número ingresado no existe.'));
}
localStorage.setItem('successMessage', 'Te has logueado satisfactoriamente.')
return done(null, user);
}));
登录成功后,从successRedirect
:
router.post("/login", passport.authenticate("local-signin",
{
successRedirect: "http://localhost:3001",
failureRedirect: "/users/login",
passReqToCallback: true
}
));
澄清:successRedirect:" http: // localhost: 3001 "
只是为了让护照不会给我任何错误,但我根本没有真正使用它。我正在通过如下所示的 window.location.href 进行重定向。
最后是我的自定义挂钩 useFormHelper
,我在其中重定向前端服务器以呈现初始页面。
if(type === "login") {
e.preventDefault();
await axios.post('http://localhost:3000/users/login', form).then(async function (response){
alert("Logueo completado satisfactoriamente.")
window.location.href = "http://localhost:3001/";
});
}
那么我的问题是,我如何将登录 passport 的用户从后端服务器本身发送到 React 前端服务器?
我在类似的 post 中看到我可以使用我的 windows.location.href 并将其传递给用户,例如:window.location.href =" http: // localhost: 3001 / "+ user
,但理论上它不是很安全。
我还想到通过 succesRedirect 发送用户,但我没有 'req' 来发送护照 req.user。 successRedirect:" http: // localhost: 3001 "+ req.user,
我希望我的解释是正确的,不要犹豫问我任何问题。谢谢。
在 router.post
中添加一个回调函数,您将在其中将 req 和 res 作为参数:
router.post("/login", passport.authenticate("local-signin",
{
successRedirect: "http://localhost:3001",
failureRedirect: "/users/login",
passReqToCallback: true
}
), (req, res)=>{
// If you use "Content-Type": "application/json"
// req.isAuthenticated is true if authentication was success else it is false
res.json({auth: req.isAuthenticated()});
});
在 then 函数的前端你可以有这样的参数:
axios.post(Your code).then((res)=>res.data.auth); // res.data will be the object that was sent from backend