Skip to content

Commit 8057709

Browse files
committed
80
1 parent 8dab248 commit 8057709

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/App.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,28 @@ import NavBar from './components/NavBar';
77
import routes from './routes';
88
import Toast from './components/Toast';
99
import useToast from './hooks/toast';
10-
import { useSelector } from 'react-redux';
10+
import { useDispatch, useSelector } from 'react-redux';
1111
import ProtectedRoute from './ProtectedRoute';
12+
import { useEffect, useState } from 'react';
13+
import { login } from './store/authSlice';
14+
import LoadingSpinner from './components/LoadingSpinner';
1215

1316
function App() {
1417
const toasts = useSelector(state => state.toast.toasts);
1518
const { deleteToast } = useToast();
19+
const [loading, setLoading] = useState(true);
20+
const dispatch = useDispatch();
21+
useEffect(() => {
22+
if (localStorage.getItem('isLoggedIn')) {
23+
dispatch(login());
24+
}
25+
setLoading(false);
26+
}, []);
27+
28+
if (loading) {
29+
return <LoadingSpinner />
30+
}
31+
1632
return (
1733
<Router>
1834
<NavBar />

src/store/authSlice.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ const authSlice = createSlice({
99
initialState,
1010
reducers: {
1111
login: (state) => {
12+
localStorage.setItem('isLoggedIn', 'yes');
1213
state.isLoggedIn = true;
1314
},
1415
logout: (state) => {
16+
localStorage.removeItem('isLoggedIn');
1517
state.isLoggedIn = false;
1618
}
1719
}

0 commit comments

Comments
 (0)