From 841f1f9dc067b2062189ed1447fb547979188b63 Mon Sep 17 00:00:00 2001 From: Raul Kele Date: Tue, 12 Jul 2022 00:33:39 +0300 Subject: [PATCH] Replaced user-pass storage with token Signed-off-by: Raul Kele --- src/App.js | 14 ++++++-------- src/api.js | 6 ++---- src/components/Header.jsx | 4 +--- src/components/SignIn.jsx | 13 +++++++------ src/pages/LoginPage.jsx | 4 ++-- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/App.js b/src/App.js index a513bdc5..d5594ada 100644 --- a/src/App.js +++ b/src/App.js @@ -1,7 +1,6 @@ import React, { useState } from 'react' import HomePage from './pages/HomePage.jsx' import LoginPage from './pages/LoginPage.jsx' -import RepoDetails from './components/RepoDetails.jsx' import makeStyles from '@mui/styles/makeStyles'; @@ -17,16 +16,15 @@ const useStyles = makeStyles((theme) => ({ })); function App() { - const isUsername = () => { - const localStorageUsername = localStorage.getItem('username'); - return localStorageUsername ? true : false; + const isToken = () => { + const localStorageToken = localStorage.getItem('token'); + return localStorageToken ? true : false; }; - const [username, setUsername] = useState(null); - const [password, setPassword] = useState(null); + const [searchKeywords, setSearchKeywords] = useState(null); const [data, setData] = useState(null); const [isAuthEnabled, setIsAuthEnabled] = useState(true) - const [isLoggedIn, setIsLoggedIn] = useState(isUsername()) + const [isLoggedIn, setIsLoggedIn] = useState(isToken()) const classes = useStyles(); @@ -42,7 +40,7 @@ function App() { } /> }> - } /> + } /> } /> diff --git a/src/api.js b/src/api.js index 52b4dcd0..156a4974 100644 --- a/src/api.js +++ b/src/api.js @@ -9,10 +9,8 @@ const api = { Accept: 'application/json', 'Content-Type': 'application/json', }; - const username = localStorage.getItem('username'); - if(username) { - const password = localStorage.getItem('password'); - const token = btoa(username + ':' + password); + const token = localStorage.getItem('token'); + if(token) { const authHeaders = { Accept: 'application/json', 'Content-Type': 'application/json', diff --git a/src/components/Header.jsx b/src/components/Header.jsx index e40129f7..27d5a2da 100644 --- a/src/components/Header.jsx +++ b/src/components/Header.jsx @@ -76,9 +76,7 @@ function Header({ updateKeywords }) { }; const handleClose = (event) => { - localStorage.removeItem('host'); - localStorage.removeItem('username'); - localStorage.removeItem('password'); + localStorage.removeItem('token'); window.location.reload(); if (anchorRef.current && anchorRef.current.contains(event.target)) { return; diff --git a/src/components/SignIn.jsx b/src/components/SignIn.jsx index c20c78ef..46835eac 100644 --- a/src/components/SignIn.jsx +++ b/src/components/SignIn.jsx @@ -43,9 +43,11 @@ const useStyles = makeStyles((theme) => ({ -export default function SignIn({ username, updateUsername, password, updatePassword, isAuthEnabled, setIsAuthEnabled, isLoggedIn, setIsLoggedIn }) { +export default function SignIn({ isAuthEnabled, setIsAuthEnabled, isLoggedIn, setIsLoggedIn }) { const [usernameError, setUsernameError] = useState(null); const [passwordError, setPasswordError] = useState(null); + const [username, setUsername] = useState(null); + const [password, setPassword] = useState(null); const [requestProcessing, setRequestProcessing] = useState(false); const [requestError, setRequestError] = useState(false); const navigate = useNavigate(); @@ -83,9 +85,8 @@ export default function SignIn({ username, updateUsername, password, updatePassw .then(response => { if (response.data && response.data.data) { if(isAuthEnabled) { - localStorage.setItem('username', username); - localStorage.setItem('password', password); - + const token = btoa(username + ':' + password); + localStorage.setItem('token',token); setRequestProcessing(false); setRequestError(false); } @@ -108,7 +109,7 @@ export default function SignIn({ username, updateUsername, password, updatePassw switch (type) { case 'username': - updateUsername(val); + setUsername(val); if (isEmpty) { setUsernameError('Please enter a username'); } else { @@ -116,7 +117,7 @@ export default function SignIn({ username, updateUsername, password, updatePassw } break; case 'password': - updatePassword(val); + setPassword(val); if (isEmpty) { setPasswordError('Please enter a password'); } else { diff --git a/src/pages/LoginPage.jsx b/src/pages/LoginPage.jsx index 949f3dc4..df56a8b3 100644 --- a/src/pages/LoginPage.jsx +++ b/src/pages/LoginPage.jsx @@ -15,13 +15,13 @@ const useStyles = makeStyles((theme) => ({ }, })); -function LoginPage({username, updateUsername, password, updatePassword, isAuthEnabled, setIsAuthEnabled, isLoggedIn,setIsLoggedIn }) { +function LoginPage({isAuthEnabled, setIsAuthEnabled, isLoggedIn,setIsLoggedIn }) { const classes = useStyles(); return ( - +