From d547ea8b3acffa30fa44e0715661490d066bf580 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Mon, 15 Jun 2020 16:51:04 +0300
Subject: feat: create logIn function and get access token

---
 src/index.tsx | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

(limited to 'src/index.tsx')

diff --git a/src/index.tsx b/src/index.tsx
index 876491d..b9204b6 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -14,7 +14,7 @@ import ProfilePage from './pages/ProfilePage/ProfilePage';
 import FeedPage from './pages/FeedPage/FeedPage';
 import AuthPage from './pages/AuthPage/AuthPage';
 import { User, Page } from './types';
-import { get } from './requests';
+import { get, post } from './requests';
 
 
 const theme = createMuiTheme({
@@ -52,9 +52,26 @@ const App: React.FC = () => {
     }
   };
 
+  const logIn = (name: string, password: string) => {
+    post('/authentication', {
+      strategy: 'local',
+      name,
+      password
+    }).then(response => {
+      const user = response.data.undefined; // TODO: rename 'undefined' field
+      const { accessToken } = response.data;
+
+      setUser(user);
+      localStorage.setItem('userId', user._id);
+      localStorage.setItem('token', accessToken);
+      navigate('profile', user._id);
+    });
+  };
+
   const logOut = () => {
-    localStorage.removeItem('userId');
     setUser(undefined);
+    localStorage.removeItem('userId');
+    localStorage.removeItem('token');
     navigate('auth');
   };
 
@@ -74,7 +91,7 @@ const App: React.FC = () => {
       <div className={classes.root}>
         { page.prefix === 'profile' && <ProfilePage logOut={logOut} id={page.id} navigate={navigate} /> }
         { page.prefix === 'feed' && <FeedPage navigate={navigate} /> }
-        { page.prefix === 'auth' && <AuthPage setUser={setUser} navigate={navigate} /> }
+        { page.prefix === 'auth' && <AuthPage logIn={logIn} navigate={navigate} /> }
       </div>
     </ThemeProvider>
   );
-- 
cgit v1.2.3

