如何在React-Redux操作中使用session auth?

时间:2017-12-19 11:24:56

标签: javascript reactjs

我想知道如何为React-Redux进行会话身份验证

这是我对react-redux的行动:

xy_dat[..]

这是我的渲染登录页面的代码

export function UserLogin(values, callback) {
    const request = axios.post(`${ROOT_URL}/user/login`, values).then(
        () => {callback();}
    ).catch((error) => {
        // Error
        if (error.response) {

             console.log(error.response.data);

        } else if (error.request) {
            // The request was made but no response was received
            // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
            // http.ClientRequest in node.js
            console.log(error.request);
        } else {
            // Something happened in setting up the request that triggered an Error
            console.log('Error', error.message);
        }
    });
    return {
        type: CREATE_POST,
        payload: request
    };
}

我想知道在React中将会话存储放在哪里?

2 个答案:

答案 0 :(得分:0)

对于会话管理,您可以将会话信息保存在Global reducer中,该reducer可以从应用程序的任何位置访问。

    export default function createReducer(asyncReducers) {
  return combineReducers({
    route: routeReducer,
    global: globalReducer,
    language: languageProviderReducer,
    ...asyncReducers,
  });
}

对于持久性,您可以添加redux-persist-immutable或redux-persist。

答案 1 :(得分:0)

经过一些研究后,我用Cookie创建了Redux。

export default function user(state = {}, action) {
    switch (action.type) {
      case LOGIN_REQUEST_SUCCESS:
        cookie.save('sessionId', action.user.sid, { path: '/' })
        return merge({}, state, {
          sessionId: action.user.sid,
          id: action.user.id,
          firstName: action.user.first_name,
          lastName: action.user.last_name,
          isAuthenticated: true
        });

在IN登录中我插入  :

return {
        type: LOGIN_REQUEST_SUCCESS,
        payload: request
    };

现在我需要获取cookie值。