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

问题描述 投票:0回答:2

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

这是我对react-redux的行为:

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

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

Render(){
  const {isAuthenticated} = this.props.auth;
}

我想知道我在React中放置会话存储的位置?

javascript reactjs
2个回答
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值。


0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.