mirror of
				https://github.com/Telecominfraproject/wlan-cloud-ui.git
				synced 2025-10-31 18:57:59 +00:00 
			
		
		
		
	fixed User Provider and token
This commit is contained in:
		| @@ -12,7 +12,7 @@ import Login from 'containers/Login'; | |||||||
| import ClientDevices from 'containers/ClientDevices'; | import ClientDevices from 'containers/ClientDevices'; | ||||||
| import UserProvider from 'contexts/UserProvider'; | import UserProvider from 'contexts/UserProvider'; | ||||||
|  |  | ||||||
| import { getItem } from 'utils/localStorage'; | import { getItem, setItem } from 'utils/localStorage'; | ||||||
| import { parseJwt } from 'utils/jwt'; | import { parseJwt } from 'utils/jwt'; | ||||||
|  |  | ||||||
| import UnauthenticatedRoute from './components/UnauthenticatedRoute'; | import UnauthenticatedRoute from './components/UnauthenticatedRoute'; | ||||||
| @@ -27,26 +27,34 @@ const RedirectToDashboard = () => ( | |||||||
| ); | ); | ||||||
|  |  | ||||||
| const App = () => { | const App = () => { | ||||||
|  |   const token = getItem(AUTH_TOKEN); | ||||||
|   const [user, setUser] = useState({}); |   const [user, setUser] = useState({}); | ||||||
|  |  | ||||||
|   useEffect(() => { |   useEffect(() => { | ||||||
|     const token = getItem(AUTH_TOKEN); |  | ||||||
|     if (token) { |     if (token) { | ||||||
|       const { userId, userName, userRole, customerId } = parseJwt(token.access_token); |       const { userId, userName, userRole, customerId } = parseJwt(token.access_token); | ||||||
|  |  | ||||||
|       setUser({ id: userId, email: userName, role: userRole, customerId }); |       setUser({ id: userId, email: userName, role: userRole, customerId }); | ||||||
|     } |     } | ||||||
|   }, []); |   }, []); | ||||||
|  |  | ||||||
|  |   const updateToken = newToken => { | ||||||
|  |     setItem(AUTH_TOKEN, newToken); | ||||||
|  |     if (newToken) { | ||||||
|  |       const { userId, userName, userRole, customerId } = parseJwt(newToken.access_token); | ||||||
|  |       setUser({ id: userId, email: userName, role: userRole, customerId }); | ||||||
|  |     } | ||||||
|  |   }; | ||||||
|  |  | ||||||
|   const updateUser = newUser => setUser({ ...user, ...newUser }); |   const updateUser = newUser => setUser({ ...user, ...newUser }); | ||||||
|  |  | ||||||
|   return ( |   return ( | ||||||
|     <UserProvider |     <UserProvider | ||||||
|       id={user.id} |       id={user.userId} | ||||||
|       email={user.email} |       email={user.userName} | ||||||
|       role={user.role} |       role={user.userRole} | ||||||
|       customerId={user.customerId} |       customerId={user.customerId} | ||||||
|       updateUser={updateUser} |       updateUser={updateUser} | ||||||
|  |       updateToken={updateToken} | ||||||
|     > |     > | ||||||
|       <ThemeProvider company={COMPANY} logo={logo} logoMobile={logoMobile}> |       <ThemeProvider company={COMPANY} logo={logo} logoMobile={logoMobile}> | ||||||
|         <Helmet titleTemplate={`%s - ${COMPANY}`} defaultTitle={COMPANY}> |         <Helmet titleTemplate={`%s - ${COMPANY}`} defaultTitle={COMPANY}> | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| import React from 'react'; | import React, { useContext } from 'react'; | ||||||
| import gql from 'graphql-tag'; | import gql from 'graphql-tag'; | ||||||
| import { useMutation, useApolloClient } from '@apollo/react-hooks'; | import { useMutation, useApolloClient } from '@apollo/react-hooks'; | ||||||
| import { useHistory } from 'react-router-dom'; | import { useHistory } from 'react-router-dom'; | ||||||
| @@ -6,9 +6,7 @@ import { notification } from 'antd'; | |||||||
|  |  | ||||||
| import { Login as LoginPage } from '@tip-wlan/wlan-cloud-ui-library'; | import { Login as LoginPage } from '@tip-wlan/wlan-cloud-ui-library'; | ||||||
|  |  | ||||||
| import { AUTH_TOKEN } from 'constants/index'; | import UserContext from 'contexts/UserContext'; | ||||||
|  |  | ||||||
| import { setItem } from 'utils/localStorage'; |  | ||||||
|  |  | ||||||
| const AUTHENTICATE_USER = gql` | const AUTHENTICATE_USER = gql` | ||||||
|   mutation AuthenticateUser($email: String!, $password: String!) { |   mutation AuthenticateUser($email: String!, $password: String!) { | ||||||
| @@ -22,6 +20,7 @@ const AUTHENTICATE_USER = gql` | |||||||
|  |  | ||||||
| const Login = () => { | const Login = () => { | ||||||
|   const history = useHistory(); |   const history = useHistory(); | ||||||
|  |   const { updateToken } = useContext(UserContext); | ||||||
|   const client = useApolloClient(); |   const client = useApolloClient(); | ||||||
|   const [authenticateUser] = useMutation(AUTHENTICATE_USER); |   const [authenticateUser] = useMutation(AUTHENTICATE_USER); | ||||||
|  |  | ||||||
| @@ -29,7 +28,7 @@ const Login = () => { | |||||||
|     authenticateUser({ variables: { email, password } }) |     authenticateUser({ variables: { email, password } }) | ||||||
|       .then(({ data }) => { |       .then(({ data }) => { | ||||||
|         client.resetStore(); |         client.resetStore(); | ||||||
|         setItem(AUTH_TOKEN, data.authenticateUser, data.authenticateUser.expires_in); |         updateToken(data.authenticateUser); | ||||||
|         history.push('/'); |         history.push('/'); | ||||||
|       }) |       }) | ||||||
|       .catch(() => |       .catch(() => | ||||||
|   | |||||||
| @@ -3,8 +3,8 @@ import PropTypes from 'prop-types'; | |||||||
|  |  | ||||||
| import UserContext from 'contexts/UserContext'; | import UserContext from 'contexts/UserContext'; | ||||||
|  |  | ||||||
| const UserProvider = ({ children, id, email, role, customerId, updateUser }) => ( | const UserProvider = ({ children, id, email, role, customerId, updateUser, updateToken }) => ( | ||||||
|   <UserContext.Provider value={{ id, email, role, customerId, updateUser }}> |   <UserContext.Provider value={{ id, email, role, customerId, updateUser, updateToken }}> | ||||||
|     {children} |     {children} | ||||||
|   </UserContext.Provider> |   </UserContext.Provider> | ||||||
| ); | ); | ||||||
| @@ -12,6 +12,7 @@ const UserProvider = ({ children, id, email, role, customerId, updateUser }) => | |||||||
| UserProvider.propTypes = { | UserProvider.propTypes = { | ||||||
|   children: PropTypes.node.isRequired, |   children: PropTypes.node.isRequired, | ||||||
|   updateUser: PropTypes.func.isRequired, |   updateUser: PropTypes.func.isRequired, | ||||||
|  |   updateToken: PropTypes.func.isRequired, | ||||||
|   id: PropTypes.number, |   id: PropTypes.number, | ||||||
|   email: PropTypes.string, |   email: PropTypes.string, | ||||||
|   role: PropTypes.string, |   role: PropTypes.string, | ||||||
|   | |||||||
| @@ -13,7 +13,9 @@ export const getItemExpiration = () => { | |||||||
|  |  | ||||||
| export const setItem = (key, data, expiration) => { | export const setItem = (key, data, expiration) => { | ||||||
|   const localStorageState = data; |   const localStorageState = data; | ||||||
|  |   if (localStorageState) { | ||||||
|     localStorageState.expiration = expiration || getItemExpiration(); |     localStorageState.expiration = expiration || getItemExpiration(); | ||||||
|  |   } | ||||||
|   window.localStorage.setItem(key, JSON.stringify(localStorageState)); |   window.localStorage.setItem(key, JSON.stringify(localStorageState)); | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Sean Macfarlane
					Sean Macfarlane