This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Auth0.tsx
57 lines (50 loc) · 1.61 KB
/
Auth0.tsx
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
import React from 'react';
import { useAuth0, Auth0Provider, Auth0ProviderOptions } from '@auth0/auth0-react';
import { IAuth } from '../../interfaces/IAuth';
import { AuthContext } from './AuthProvider';
import PageLoader from '../ContentState/PageLoader';
export const Provider = Auth0Provider;
export const ProviderOptions: Auth0ProviderOptions = {
domain: process.env.AUTH0_DOMAIN || '',
clientId: process.env.AUTH0_CLIENT_ID || '',
redirectUri: process.env.AUTH0_REDIRECT_URI || window.location.origin,
cacheLocation: 'localstorage',
};
const AuthConfigurations: React.FC = ({ children }) => {
const { isLoading, isAuthenticated, loginWithRedirect, logout, user, getIdTokenClaims } = useAuth0();
if (!isLoading && !isAuthenticated) {
loginWithRedirect();
}
const auth0Logout = () => {
return logout({ returnTo: process.env.AUTH0_LOGOUT_URI || window.location.origin });
};
const getJwt = async (): Promise<string> => {
if (isAuthenticated) {
const token = await getIdTokenClaims();
return token.__raw;
}
return new Promise((_, __) => '');
};
const auth: IAuth = {
loginWithRedirect: loginWithRedirect,
logout: auth0Logout,
isAuthenticated: isAuthenticated,
isReady: !isLoading,
user: {
id: user?.sub,
name: user?.name,
email: user?.email,
jwt: getJwt,
},
};
if (auth.isReady && !auth.isAuthenticated) {
loginWithRedirect();
return <PageLoader />;
}
return (
<React.Fragment>
<AuthContext.Provider value={auth}>{children}</AuthContext.Provider>
</React.Fragment>
);
};
export default AuthConfigurations;