action.js
:
export const login = creds => {
console.log(`${url}/login`);
const requestOptions = {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: creds
};
return function(dispatch) {
dispatch({ type: LOGIN_REQUEST });
function timer() {
return fetch(`${url}/login`, requestOptions).then(response => {
if (!response.ok) {
console.log(response);
return response.json().then(json => {
var error = new Error(json.message);
error.response = response;
throw error;
});
} else {
console.log("3");
return response.json();
}
}).then(user => {
if (user.message === "ok") {
localStorage.setItem("token", user.token);
dispatch({ type: LOGIN_SUCCESS, payload: user.token });
window.location.href = `${app}/dashboard`;
} else {
const error = user.message;
throw new Error(error);
}
}).catch(function(error) {
dispatch(loginError(error));
});
}
setTimeout(timer, 5000)
}
};
我无法以单页方式重定向到我的仪表板,我搜索了很多,但没有得到任何有用的东西。我正在使用 React Router v4。您能否建议我是否以正确的方式使用 JWT 进行此用户登录。
创建一个你自己的history
in history.js
使用这个文件历史图书馆 https://github.com/ReactTraining/history.
//history.js
import createHistory from 'history/createBrowserHistory'
const history = createHistory()
export default history
将其提供给您的路由器:
<Router history = {history}>.....</Router>
然后你可以使用这个history
对象从任何地方重定向。在你的行动中:
import history from './history'
history.push(`${app}/dashboard`)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)