Skip to content

Commit

Permalink
Refactor auth persistence logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SafdarJamal committed Jun 22, 2022
1 parent 2a39709 commit 396d546
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/components/App/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';

import Login from '../Login';
import Dashboard from '../Dashboard';

const App = () => {
const [isAuthenticated, setIsAuthenticated] = useState(
JSON.parse(localStorage.getItem('isAuthenticated'))
);
const [isAuthenticated, setIsAuthenticated] = useState(null);

useEffect(() => {
setIsAuthenticated(JSON.parse(localStorage.getItem('is_authenticated')));
}, []);

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Login = ({ setIsAuthenticated }) => {
Swal.showLoading();
},
willClose: () => {
localStorage.setItem('isAuthenticated', true);
localStorage.setItem('is_authenticated', true);
setIsAuthenticated(true);

Swal.fire({
Expand Down
2 changes: 1 addition & 1 deletion src/components/Logout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const Logout = ({ setIsAuthenticated }) => {
Swal.showLoading();
},
willClose: () => {
localStorage.setItem('isAuthenticated', false);
localStorage.setItem('is_authenticated', false);
setIsAuthenticated(false);
},
});
Expand Down

0 comments on commit 396d546

Please sign in to comment.