From 58ebf27cf14641c1e894876a19d258bf2da1bfd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20H=E1=BA=A3i=20D=C5=A9ng?= Date: Wed, 21 Dec 2022 14:26:16 +0700 Subject: [PATCH] fix link token --- app/src/components/AutoLogin/AutoLogin.js | 24 +++++++++++++++++++++-- app/src/components/layouts/Header.js | 1 + app/src/components/layouts/MenuBar.js | 18 ++++++++--------- app/src/reducers/role/index.js | 4 +++- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/app/src/components/AutoLogin/AutoLogin.js b/app/src/components/AutoLogin/AutoLogin.js index e64fe18..a6de045 100644 --- a/app/src/components/AutoLogin/AutoLogin.js +++ b/app/src/components/AutoLogin/AutoLogin.js @@ -3,6 +3,7 @@ import React, { Component } from 'react'; import "react-datepicker/dist/react-datepicker.css"; import { HOST } from '../../config/index'; import Store from '../../store'; +import { role } from '../../actions/role'; class AutoLogin extends Component { constructor(props) { super(props); @@ -24,11 +25,30 @@ class AutoLogin extends Component { if(this.props.match.params.token){ localStorage.setItem("access_token", "Bearer " + this.props.match.params.token); Store.dispatch(login("Bearer " + this.props.match.params.token)); - window.location.href = "/"; + let data = fetch(`${HOST}/api/get_info`, { + method: 'GET', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'Authorization': this.props.match.params.token, + // 'Authorization': token, + }, + }).then((response) => { + return response.json() + }).then((data) => { + if(data.status === 10000){ + localStorage.setItem("roles", JSON.stringify(data.roles)); + Store.dispatch(role(data.roles)); + window.location.href = "/"; + }else{ + alert('Sai token!!!'); + window.location.href = "/login"; + } + }) } } render() { - console.log('oke',this.props.match) + console.log('oke',Store.getState().role.role) return(
) diff --git a/app/src/components/layouts/Header.js b/app/src/components/layouts/Header.js index 303d728..d6ed308 100644 --- a/app/src/components/layouts/Header.js +++ b/app/src/components/layouts/Header.js @@ -37,6 +37,7 @@ class Header extends Component { }).then(data => { if (data.status === 10000) { Store.dispatch(setbox(data.data.company_id)); + localStorage.setItem("roles", JSON.stringify(data.data.roles)); Store.dispatch(role(data.data.roles)); this.setState({ full_name: data.data.full_name diff --git a/app/src/components/layouts/MenuBar.js b/app/src/components/layouts/MenuBar.js index 5089680..68eab50 100644 --- a/app/src/components/layouts/MenuBar.js +++ b/app/src/components/layouts/MenuBar.js @@ -97,10 +97,10 @@ class MenuBar extends Component { : ""} - {/* {this.state.dataRole?.indexOf('user:view') !== -1 ? + {this.state.dataRole?.indexOf('user:view') !== -1 ?
  • this.onClickClose()}> - + Quản lý người dùng @@ -108,12 +108,12 @@ class MenuBar extends Component {
  • - : ""} */} + : ""} - {/* {this.state.dataRole?.indexOf('level:view') !== -1 ? + {this.state.dataRole?.indexOf('level:view') !== -1 ?
  • this.onClickClose()}> - + Thiết lập quyền @@ -121,12 +121,12 @@ class MenuBar extends Component {
  • - : ""} */} + : ""} - {/* {this.state.dataRole?.indexOf('level:view') !== -1 ? + {this.state.dataRole?.indexOf('level:view') !== -1 ?
  • this.onClickClose()}> - + Độ chính xác @@ -134,7 +134,7 @@ class MenuBar extends Component {
  • - : ""} */} + : ""} {/* END: Aside Menu */} diff --git a/app/src/reducers/role/index.js b/app/src/reducers/role/index.js index b0a6a58..5c11e3b 100644 --- a/app/src/reducers/role/index.js +++ b/app/src/reducers/role/index.js @@ -1,7 +1,9 @@ import {ROLE} from '../../actions/role'; +let roles = JSON.parse(localStorage.getItem('roles')); + const initialState = { - role: null + role: roles }; export default function role(state = initialState, action) { switch(action.type){