【发布时间】:2021-08-01 16:15:18
【问题描述】:
我正在尝试使用我在 nodejs 中使用护照模块制作的 REST api 进行简单的登录身份验证。 我的前端调用了这个函数,但是这个函数返回一个错误并且没有发生身份验证。帖子 fn 不起作用。
export const loginUser = (creds) => (dispatch) => {
// We dispatch requestLogin to kickoff the call to the API
dispatch(requestLogin(creds))
return fetch(baseUrl + 'users/login', {
method: 'POST',
headers: {
'Content-Type':'application/json'
},
body: JSON.stringify(creds)
})
.then(response => {
console.log("HELLO");
if (response.ok) {
return response;
} else {
var error = new Error('Error ' + response.status + ': ' + response.statusText);
error.response = response;
throw error;
}
},
error => {
throw error;
})
.then(response => response.json())
.then(response => {
console.log("HELLO2");
if (response.success) {
// If login was successful, set the token in local storage
localStorage.setItem('token', response.token);
localStorage.setItem('creds', JSON.stringify(creds));
console.log(response);
// Dispatch the success action
// dispatch(fetchFavorites());
dispatch(receiveLogin(response));
}
else {
var error = new Error('Error ' + response.status);
error.response = response;
throw error;
}
})
.catch(error => dispatch(loginError(error.message)))
};
【问题讨论】:
标签: javascript node.js reactjs rest redux