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

react-router redux how can i update state on load of page for authentication

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

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

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

依我看,我需要考虑两种情况

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

我的问题是使用初学者工具包我不知道如何正确在我的路线文件中到达state/store所以我可以检查state.auth.user 属性.. 或者这是否是正确的方法(也许我应该改用动作)?

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>);

我一直在 https://github.com/davezuko/react-redux-starter-kit

中使用此解决方案
  1. 创建 AuthenticatedComponent https://github.com/joshgeller/react-redux-jwt-auth-example/blob/master/src/components/AuthenticatedComponent.js

  2. 配置路由器组件需要身份验证:https://github.com/joshgeller/react-redux-jwt-auth-example/blob/master/src/routes/index.js

  3. 检查令牌(页面加载):https://github.com/joshgeller/react-redux-jwt-auth-example/blob/master/src/index.js