Skip to content

Commit 112662f

Browse files
authored
fix: redirects (#34)
1 parent 41e272a commit 112662f

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/api/ApiConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export const apiConfig = new Configuration({
2727
post: async context => {
2828
const statusCode = context.response.status;
2929
if (statusCode === 401) {
30+
localStorage.removeItem('token');
3031
window.location.href = `${homeUrl}/#/login`;
3132
window.history.forward();
33+
window.location.reload();
3234
} else if (statusCode === 403) {
3335
const authApi = new AuthApi(apiConfig);
3436
authApi.getAuthStatus().then(res => {

src/components/Auth/Auth/Login/Login.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ import styles from '../auth.module.css';
55
import { useNavigate } from 'react-router-dom';
66
import { faChevronRight } from '@fortawesome/free-solid-svg-icons';
77
import { BASE_PATH } from '../../../../config/config';
8-
import { loginAction, loginError } from '../../../../store/User/UserSlice';
8+
import {
9+
isloggedIn,
10+
loginAction,
11+
loginError,
12+
loading,
13+
} from '../../../../store/User/UserSlice';
914
import { useAppSelector, useAppDispatch } from '../../../../store/hooks';
1015
import { IconProp } from '@fortawesome/fontawesome-svg-core';
1116
import ForgetPassword from './ForgetPassword/ForgetPassword';
1217
import toast from 'react-hot-toast';
1318

1419
function Login(): JSX.Element {
1520
const navigate = useNavigate();
21+
const isLoggedIn = useAppSelector(isloggedIn);
22+
const loadingAuth = useAppSelector(loading);
1623
const [email, setEmail] = useState('');
1724
const [password, setPassword] = useState('');
1825
const [open, isOpen] = useState<boolean>(false);
@@ -23,10 +30,10 @@ function Login(): JSX.Element {
2330
}, [loggedInError]);
2431

2532
useEffect(() => {
26-
if (localStorage.getItem('token') != null) {
33+
if (isLoggedIn && !loadingAuth) {
2734
navigate('/dashboard', { replace: true });
2835
}
29-
}, [localStorage.getItem('token')]);
36+
}, [isLoggedIn, loadingAuth]);
3037
const handleForgetPassword = () => {
3138
if (open == false) isOpen(true);
3239
else isOpen(false);

src/components/NavBar/NavBar.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
loggedIn,
1010
user,
1111
logout,
12+
loading,
1213
} from '../../store/User/UserSlice';
1314
import { useAppDispatch, useAppSelector } from '../../store/hooks';
1415
import { AuthApi } from '@codecharacter-2023/client';
@@ -35,6 +36,7 @@ const NavBar: React.FunctionComponent = () => {
3536
const location = useLocation();
3637
const loggedInUser = useAppSelector(user);
3738
const isLogged = useAppSelector(isloggedIn);
39+
const loadingAuth = useAppSelector(loading);
3840
const dcCompletionstatus = useAppSelector(dailyChallengeCompletionState);
3941
useEffect(() => {
4042
const cookieValue = document.cookie;
@@ -140,8 +142,17 @@ const NavBar: React.FunctionComponent = () => {
140142
</NavLink>
141143
</div>
142144
)}
145+
{location.pathname === '/' && isLogged && (
146+
<div className={styles.navContainer}>
147+
<NavLink to="/dashboard" className={`${styles.navLink}`}>
148+
<img src={signInIcon} />
149+
Dashboard
150+
</NavLink>
151+
</div>
152+
)}
143153
</div>
144-
{localStorage.getItem('token') != null &&
154+
{isLogged &&
155+
!loadingAuth &&
145156
location.pathname != '/incomplete-profile' &&
146157
location.pathname != '/' ? (
147158
<div className={styles.profileIcons}>

0 commit comments

Comments
 (0)