diff --git a/src/features/auth/authSlice.js b/src/features/auth/authSlice.js index 5823c91..92146e1 100644 --- a/src/features/auth/authSlice.js +++ b/src/features/auth/authSlice.js @@ -9,11 +9,13 @@ export const authSlice = createSlice({ name: 'auth', initialState, reducers: { - authLogin: (state, action) => { + authLogin: state => { state.isAuthenticated = true; + Cookies.set('authorized', true); }, authLogout: state => { state.isAuthenticated = false; + Cookies.set('authorized', false); }, }, }); diff --git a/src/screens/LoginScreen.js b/src/screens/LoginScreen.js index c7d0789..833aba8 100644 --- a/src/screens/LoginScreen.js +++ b/src/screens/LoginScreen.js @@ -10,7 +10,7 @@ import axios from 'axios'; import { React, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { Redirect, useHistory } from 'react-router-dom'; -import { authLogin } from '../features/auth/authSlice'; +import { authLogin, authLogout } from '../features/auth/authSlice'; function Login() { const [email, setEmail] = useState(null); @@ -21,7 +21,7 @@ function Login() { const isAuthenticated = useSelector(state => state.auth.isAuthenticated); if (isAuthenticated) return ; - + const handleSubmit = e => { if (!email | !password) { toast({ @@ -57,6 +57,7 @@ function Login() { dispatch(authLogin()); history.push('/home'); } else { + dispatch(authLogout()); toast({ title: 'An error occurred', description: response.data.message, @@ -67,6 +68,7 @@ function Login() { } }) .catch(err => { + dispatch(authLogout()); toast({ title: 'An error occurred', description: 'Sorry, an error occurred on our side.',