简体   繁体   English

如何将会话身份验证与React-Redux操作一起使用?

[英]How to use session auth with React-Redux actions?

I want to know how to make session auth for React-Redux 我想知道如何对React-Redux进行会话身份验证

This is my actions for 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
    };
}

This is my code for Render login Page 这是我的渲染登录页面代码

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

I want to know where i put session storage in React? 我想知道我将会话存储放在React的什么地方?

For Session management you can persist the session information in a Global reducer, which can be accessible from any point of the application. 对于会话管理,您可以将会话信息保留在Global reducer中,可以从应用程序的任何位置进行访问。

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

For persistence, you can add redux-persist-immutable or redux-persist. 为了保持持久性,您可以添加redux-persist-immutable或redux-persist。

After some research i have create Redux with Cookie. 经过研究后,我用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
        });

And IN Login i insert : 在登录我输入:

return {
        type: LOGIN_REQUEST_SUCCESS,
        payload: request
    };

Now i need to get the cookie value. 现在,我需要获取Cookie值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM