From fe2f30717ac6a452296316408262b3eb68d26ef7 Mon Sep 17 00:00:00 2001 From: Sean Macfarlane Date: Tue, 12 May 2020 18:30:45 -0400 Subject: [PATCH] fixed User Provider and token --- app/containers/App/index.js | 20 ++++++++++++++------ app/containers/Login/index.js | 9 ++++----- app/contexts/UserProvider/index.js | 5 +++-- app/utils/localStorage.js | 4 +++- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/app/containers/App/index.js b/app/containers/App/index.js index 6369777..c0aa7c0 100644 --- a/app/containers/App/index.js +++ b/app/containers/App/index.js @@ -12,7 +12,7 @@ import Login from 'containers/Login'; import ClientDevices from 'containers/ClientDevices'; import UserProvider from 'contexts/UserProvider'; -import { getItem } from 'utils/localStorage'; +import { getItem, setItem } from 'utils/localStorage'; import { parseJwt } from 'utils/jwt'; import UnauthenticatedRoute from './components/UnauthenticatedRoute'; @@ -27,26 +27,34 @@ const RedirectToDashboard = () => ( ); const App = () => { + const token = getItem(AUTH_TOKEN); const [user, setUser] = useState({}); useEffect(() => { - const token = getItem(AUTH_TOKEN); if (token) { const { userId, userName, userRole, customerId } = parseJwt(token.access_token); - 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 }); return ( diff --git a/app/containers/Login/index.js b/app/containers/Login/index.js index 1a6ece5..5388704 100644 --- a/app/containers/Login/index.js +++ b/app/containers/Login/index.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useContext } from 'react'; import gql from 'graphql-tag'; import { useMutation, useApolloClient } from '@apollo/react-hooks'; 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 { AUTH_TOKEN } from 'constants/index'; - -import { setItem } from 'utils/localStorage'; +import UserContext from 'contexts/UserContext'; const AUTHENTICATE_USER = gql` mutation AuthenticateUser($email: String!, $password: String!) { @@ -22,6 +20,7 @@ const AUTHENTICATE_USER = gql` const Login = () => { const history = useHistory(); + const { updateToken } = useContext(UserContext); const client = useApolloClient(); const [authenticateUser] = useMutation(AUTHENTICATE_USER); @@ -29,7 +28,7 @@ const Login = () => { authenticateUser({ variables: { email, password } }) .then(({ data }) => { client.resetStore(); - setItem(AUTH_TOKEN, data.authenticateUser, data.authenticateUser.expires_in); + updateToken(data.authenticateUser); history.push('/'); }) .catch(() => diff --git a/app/contexts/UserProvider/index.js b/app/contexts/UserProvider/index.js index 70b8198..a660bc2 100644 --- a/app/contexts/UserProvider/index.js +++ b/app/contexts/UserProvider/index.js @@ -3,8 +3,8 @@ import PropTypes from 'prop-types'; import UserContext from 'contexts/UserContext'; -const UserProvider = ({ children, id, email, role, customerId, updateUser }) => ( - +const UserProvider = ({ children, id, email, role, customerId, updateUser, updateToken }) => ( + {children} ); @@ -12,6 +12,7 @@ const UserProvider = ({ children, id, email, role, customerId, updateUser }) => UserProvider.propTypes = { children: PropTypes.node.isRequired, updateUser: PropTypes.func.isRequired, + updateToken: PropTypes.func.isRequired, id: PropTypes.number, email: PropTypes.string, role: PropTypes.string, diff --git a/app/utils/localStorage.js b/app/utils/localStorage.js index de6173c..4e75cc0 100644 --- a/app/utils/localStorage.js +++ b/app/utils/localStorage.js @@ -13,7 +13,9 @@ export const getItemExpiration = () => { export const setItem = (key, data, expiration) => { const localStorageState = data; - localStorageState.expiration = expiration || getItemExpiration(); + if (localStorageState) { + localStorageState.expiration = expiration || getItemExpiration(); + } window.localStorage.setItem(key, JSON.stringify(localStorageState)); };