mern-错误:401(未经授权)passport带有passportlocalMongoose和express
我使用Passport进行身份验证,Passport local mongoose用于插件userShema。当我尝试登录和注册使用邮递员它将成功。我登录并获得用户。但每次我用axios发送(我使用mern stack)时只会得到一个错误:mern-错误:401(未经授权)passport带有passportlocalMongoose和express,express,react-redux,axios,passport.js,passport-local-mongoose,Express,React Redux,Axios,Passport.js,Passport Local Mongoose,我使用Passport进行身份验证,Passport local mongoose用于插件userShema。当我尝试登录和注册使用邮递员它将成功。我登录并获得用户。但每次我用axios发送(我使用mern stack)时只会得到一个错误:POSThttp://localhost:3001/api/users/login 401(未经授权) 我使用redux以及如何将express中的401错误发送到后端这是 这是链接: 登录和注册 router.post('/register', async
POSThttp://localhost:3001/api/users/login 401(未经授权)
我使用redux以及如何将express中的401错误发送到后端这是 这是链接: 登录和注册
router.post('/register', async (req, res) => {
await User.register( {email: req.body.email}, req.body.password, (err,user)=>{
if(err) return res.json({ success: false, err }); // How to send error ?
else res.status(200).json({
success: true
});
});
});
router.post("/login", function (req, res) {
passport.authenticate("local")(req, res, function () {
res.json({
loginSuccess: true
});
});
res.status(401).json({ // How to send this or error ?
loginSuccess: false
})
});
这是我向后端发送数据时的前端:
export const User_Login = (value) => async (dispatch) =>{
dispatch({type: user_login_request});
await axios.post('http://localhost:3001/api/users/login', value)
.then(res => {
if(res.status === 200){
dispatch({type: user_login_success, payload: res.data});
}
else{
dispatch( {type: user_login_fail, payload: "Invalid email or password"} );
}
})
.catch(error =>{
console.log(error.status);
dispatch({type:user_login_fail, payload:error.message})
})
}
export const User_Register = (value) => async (dispatch) =>{
dispatch({type: user_register_request});
await axios.post('http://localhost:3001/api/users/login', value)
.then(res => {
if(res.status === 200){
dispatch({type: user_register_success, payload: res.data});
}
else{
dispatch( {type: user_register_fail, payload: "Email already exits"} );
}
})
.catch(error =>{
console.log(error.status);
dispatch({type:user_register_fail, payload:error.message})
})
}