gpt4 book ai didi

react-router redux 我如何更新页面加载状态以进行身份​​验证

转载 作者:行者123 更新时间:2023-12-01 10:36:26 25 4
gpt4 key购买 nike

我正在使用 https://github.com/davezuko/react-redux-starter-kit作为入门工具包并尝试将身份验证引入入门应用。

我有身份验证工作,它在登录成功时设置 state.auth,然后我在我的 protected 路由上有一个 onEnter 调用 isAuthenticated() 检查用户是否通过身份验证。

这是我迷路的地方,我不确定如何检查 state.auth.userlocalStorage.token 以确保一切都已设置。

在我看来,我需要考虑两种情况

  1. 用户已登录,但随后刷新了页面。这意味着 token 仍在 localStorage 中,但状态已被删除,因此我需要通过解码 token 并将其重新注入(inject) state.auth.user
  2. 来重新加载状态
  3. 用户未登录,将他们重定向到路由 /auth/login(这部分我正在处理)。

我的问题是使用初学者工具包我不知道如何正确在我的路由文件中获取状态/存储所以我可以检查那个state.auth.user 属性.. 或者这是否是正确的方法(也许我应该改用 Action )?

redux/modules/auth.js

import { createAction, handleActions } from 'redux-actions';
import { pushPath } from 'redux-simple-router'
// ------------------------------------
// Constants
// ------------------------------------
export const LOGIN_REQUEST = 'LOGIN_REQUEST';
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS';
export const LOGIN_FAILURE = 'LOGIN_FAILURE';
export const STORE_USER = 'STORE_USER';
export const IS_AUTHENTICATED = 'IS_AUTHENTICATED';

const initialState = {
isFetching: false,
isAuthenticated: false,
user: {},
token: ''
};

// ------------------------------------
// Actions
// ------------------------------------
export const requestLogin = createAction(LOGIN_REQUEST, (payload) => payload);
export const receiveLogin = createAction(LOGIN_SUCCESS, (payload) => payload);
export const invalidLogin = createAction(LOGIN_FAILURE, (payload) => payload);

export const isAuthenticated = () => {
return !!getToken();
};

const getToken = () => {
return localStorage.token;
};

const _decodeToken = (token) => {
return window.atob(token.split('.')[1]);
};

const storeToken = (token) => {
localStorage.token = token;
};

export const doLogin = (identity, password) => {
return (dispatch, getState) => {
dispatch(requestLogin({ identity, password }));
// mock backend call
setTimeout(function () {
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiZmlyc3ROYW1lIjoiQWRtaW4iLCJsYXN0TmFtZSI6ImlzdHJhdG9yIiwiZW1haWwiOiJhZG1pbkBhenN1cHJhcy5jb20iLCJjcmVhdGVkQXQiOiIyMDE1LTEyLTMwVDIxOjMyOjIxLjM1NloiLCJ1cGRhdGVkQXQiOiIyMDE1LTEyLTMwVDIxOjMzOjE3LjQzMloiLCJpZCI6IjU2ODQ0ZDY1Y2UzMjEyZTUwMWE3ZmNmNyIsImlhdCI6MTQ1MTUxNjU5N30.qpDmsnpMaHZy4QITS5IBPhPieNER7QHKSFWzsvulWC8';
storeToken(token);
dispatch(receiveLogin({ user: { username: 'admin', uid: 1 }, token }));
dispatch(pushPath('/'));
}, 3000);
};
};

export const actions = {
doLogin,
isAuthenticated
};

// ------------------------------------
// Reducer
// ------------------------------------
export default handleActions({
[LOGIN_REQUEST]: (state, { payload }) => {
return {
...state,
isFetching: true,
isAuthenticated: false
};
},
[LOGIN_SUCCESS]: (state, { payload }) => {
return {
...state,
isFetching: false,
isAuthenticated: true,
token: payload.token,
user: payload.user
};
},
[LOGIN_FAILURE]: (state, { payload }) => {
return {
...state,
isFetching: false,
isAuthenticated: false,
message: payload
};
}
}, initialState);

routes/index.js

import { Route, IndexRoute } from 'react-router';

// NOTE: here we're making use of the `resolve.root` configuration
// option in webpack, which allows us to specify import paths as if
// they were from the root of the ~/src directory. This makes it
// very easy to navigate to files regardless of how deeply nested
// your current file is.
import CoreLayout from 'layouts/CoreLayout';
import AuthLayout from 'layouts/AuthLayout';

import HomeView from 'views/HomeView';
import LoginView from 'views/auth/LoginView';

import { actions as authActions } from '../redux/modules/auth';

function isAuthenticated (nextState, replaceState) {
if (authActions.isAuthenticated()) {
replaceState({ nextPathname: nextState.location.pathname }, '/auth/login');
}
}

export default (<Route>
<Route path='/' component={CoreLayout} onEnter={isAuthenticated}>
<IndexRoute component={HomeView} />
<Route path='/panel' name='Panel' component={HomeView} />
</Route>
<Route path='/auth' component={AuthLayout}>
<Route path='login' component={LoginView} />
</Route>
</Route>);

最佳答案

关于react-router redux 我如何更新页面加载状态以进行身份​​验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34624257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com