From 2c6946b35aa2a756230bb96e69302c08fd8068ee Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Thu, 25 Jun 2020 21:56:33 +0300
Subject: fix: clear storage correctly

---
 src/index.tsx | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

(limited to 'src/index.tsx')

diff --git a/src/index.tsx b/src/index.tsx
index 2747bfa..a321cf4 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -55,7 +55,7 @@ const App: React.FC = () => {
     }
   };
 
-  const logIn = (username: string, password: string, remember?: boolean): Promise<boolean> => {
+  const logIn = (username: string, password: string, remember = true): Promise<boolean> => {
     return post('/authentication', {
       strategy: 'local',
       username,
@@ -65,22 +65,22 @@ const App: React.FC = () => {
       const token = response.data.accessToken;
       setUser(me);
       navigate('profile', me._id);
-      if (remember) {
-        localStorage.setItem('userId', me._id);
-        localStorage.setItem('token', token);
-      }
+      localStorage.setItem('userId', me._id);
+      localStorage.setItem('token', token);
+      if (!remember) localStorage.setItem('shouldClear', 'true');
       return true;
     }).catch(() => false);
   };
 
-  const logOut = () => {
+  const logOut = (redirect = true) => {
     setUser(undefined);
     localStorage.removeItem('userId');
     localStorage.removeItem('token');
-    navigate('auth');
+    if (redirect) navigate('auth');
   };
 
   useEffect(() => {
+    if (localStorage.getItem('shouldClear')) logOut(false);
     const userId = localStorage.getItem('userId');
     if (userId) {
       get(`/users/${userId}`).then(response => {
-- 
cgit v1.2.3

