From 52e04006df13ee1797b44a3da192a40d9f13ec49 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Sat, 22 Aug 2020 00:08:42 +0200 Subject: [PATCH] Maintain token after browser refresh --- src/App.js | 2 +- src/screens/Dashboard.js | 15 ++++++++++++--- src/screens/Home.js | 3 +++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/App.js b/src/App.js index 9e421f1..2bdc93c 100644 --- a/src/App.js +++ b/src/App.js @@ -18,7 +18,7 @@ const existingToken = localStorage.getItem('token'); // const existingUser = JSON.parse(localStorage.getItem('user')); export default function App() { - const [authToken, setAuthToken] = useState(existingToken); + const [authToken, setAuthToken] = useState(existingToken || ''); const setToken = (data) => { localStorage.setItem('token', JSON.stringify(data)); diff --git a/src/screens/Dashboard.js b/src/screens/Dashboard.js index dbbe93d..777700f 100644 --- a/src/screens/Dashboard.js +++ b/src/screens/Dashboard.js @@ -1,14 +1,23 @@ import React from 'react'; import { Panel } from 'rsuite'; +import { useAuth } from '../helpers/authContext'; +import NavBar from '../components/Navbar/NavBar'; + const boxStyle = { margin: '50px 10px', }; export default function Dashboard() { + const { authToken } = useAuth(); + return ( - Dashboard} bordered style={boxStyle}> -

This is just experimenting with stuff.

-
+ <> + + Dashboard} bordered style={boxStyle}> +

This is just experimenting with stuff.

+

{authToken}

+
+ ); } diff --git a/src/screens/Home.js b/src/screens/Home.js index 7a48f1a..7263252 100644 --- a/src/screens/Home.js +++ b/src/screens/Home.js @@ -1,6 +1,7 @@ import React from 'react'; import { Panel } from 'rsuite'; +import { useAuth } from '../helpers/authContext'; import NavBar from '../components/Navbar/NavBar'; const boxStyle = { @@ -8,11 +9,13 @@ const boxStyle = { }; export default function Home() { + const { authToken } = useAuth(); return ( <> Home} bordered style={boxStyle}>

This will be the home page

+

{authToken}

);