doc*_*pus 1 node.js express reactjs passport.js redux
我正在使用Passportjs进行用户身份验证,如果身份验证不成功,我想显示在客户端(在本例中为React / Redux)上呈现的自定义错误消息。
路线:
app.post(
'/api/login',
(req, res) => {
passport.authenticate('local-login', (err, user, info) => {
if (!user) return res.status(400).send({ msg: 'Please verify account first' });
})(req, res);
}
);
Run Code Online (Sandbox Code Playgroud)
动作创建者:
export const loginUser = (values, history) => async dispatch => {
try {
const res = await axios.post('/api/login', values);
dispatch(deleteMessage());
dispatch({ type: FETCH_USER, payload: res.data });
history.push('/');
window.Materialize.toast(`Welcome back ${res.data.local.email}!`, 4000);
}
catch(err) {
console.log('Error:', err);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我发现错误并通过控制台将其注销时,我收到标准状态400错误消息,而不是: { msg: 'Please verify account first' }
Error: Request failed with status code 400
at createError (createError.js:16)
at settle (settle.js:18)
at XMLHttpRequest.handleLoad (xhr.js:77)
Run Code Online (Sandbox Code Playgroud)
有更好的方法来显示自定义错误消息吗?
从修改
console.log(error)为console.log(error.response)。
参考:https : //github.com/axios/axios/issues/960#issuecomment-309287911
| 归档时间: |
|
| 查看次数: |
1724 次 |
| 最近记录: |