-
-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathindex.tsx
More file actions
86 lines (70 loc) · 2.35 KB
/
index.tsx
File metadata and controls
86 lines (70 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React, { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Outlet, useLocation } from 'react-router-dom';
import AppLayout from 'layouts/AppLayout';
import { useAppDispatch, useAppSelector } from 'hooks';
import { useGetUserDataQuery } from 'services/user';
import { EnterpriseLogin } from './Login/EnterpriseLogin';
import { LoginByGithub } from './Login/LoginByGithub';
import { ROUTES } from '../routes';
import { AuthErrorMessage } from './AuthErrorMessage';
import { selectAuthToken, setUserData } from './slice';
const localStorageIsAvailable = 'localStorage' in window;
const IGNORED_AUTH_PATHS = [
ROUTES.AUTH.GITHUB_CALLBACK,
ROUTES.AUTH.OKTA_CALLBACK,
ROUTES.AUTH.ENTRA_CALLBACK,
ROUTES.AUTH.GOOGLE_CALLBACK,
ROUTES.AUTH.TOKEN,
];
const LoginFormComponent = process.env.UI_VERSION === 'enterprise' ? EnterpriseLogin : LoginByGithub;
const App: React.FC = () => {
const { t } = useTranslation();
const token = useAppSelector(selectAuthToken);
const isAuthenticated = Boolean(token);
const dispatch = useAppDispatch();
const { pathname } = useLocation();
const {
isLoading,
data: userData,
error: getUserError,
} = useGetUserDataQuery(
{ token },
{
skip: !isAuthenticated || !localStorageIsAvailable,
},
);
useEffect(() => {
if (userData?.username || getUserError) {
if (userData?.username) {
dispatch(setUserData(userData));
}
}
}, [userData, getUserError, isLoading]);
const renderLocalstorageError = () => {
return (
<AuthErrorMessage
title={t('common.local_storage_unavailable')}
text={t('common.local_storage_unavailable_message')}
/>
);
};
const renderTokenError = () => {
return <LoginFormComponent />;
};
const renderNotAuthorizedError = () => {
return <LoginFormComponent />;
};
if (IGNORED_AUTH_PATHS.includes(pathname)) {
return <Outlet />;
}
if (!localStorageIsAvailable) return renderLocalstorageError();
if (getUserError) return renderTokenError();
if (!isAuthenticated) return renderNotAuthorizedError();
return (
<AppLayout>
<Outlet />
</AppLayout>
);
};
export default App;