-
Notifications
You must be signed in to change notification settings - Fork 4
/
gatsby-ssr.js
61 lines (57 loc) · 2.02 KB
/
gatsby-ssr.js
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
import React from 'react';
import { Provider } from 'react-redux';
import { Auth0Context, Auth0Provider } from '@auth0/auth0-react';
import { navigate } from 'gatsby';
import store from './src/state/store';
import AuthWrapper from './src/components/common/authWrapper';
import './src/styles/style.css';
import 'ag-grid-community/dist/styles/ag-grid.css';
import 'ag-grid-community/dist/styles/ag-theme-alpine.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import Loading from './src/components/loading';
import AuthError from './src/components/common/authError';
const onRedirectCallback = appState => navigate(appState?.returnTo || '/');
export const wrapRootElement = ({ element }) => {
return (
<Provider store={store}>
<Auth0Provider
domain={process.env.AUTH0_DOMAIN}
clientId={process.env.AUTH0_CLIENT_ID}
audience={process.env.AUTH0_AUDIENCE}
responseType="token id_token"
scope="openid profile email offline_access"
useRefreshTokens
cacheLocation="localstorage"
redirectUri={process.env.AUTH0_CALLBACK}
onRedirectCallback={onRedirectCallback}>
<Auth0Context.Consumer>
{({
isAuthenticated,
isLoading,
getAccessTokenSilently,
getIdTokenClaims,
user,
error,
logout,
}) => {
if (error)
return <AuthError message={error.message} logout={logout} />;
if (isLoading) return <Loading />;
if (!isLoading && isAuthenticated)
return (
<AuthWrapper
isLoading={isLoading}
isAuthenticated={isAuthenticated}
getAccessTokenSilently={getAccessTokenSilently}
getIdTokenClaims={getIdTokenClaims}
user={user}>
{element}
</AuthWrapper>
);
return element;
}}
</Auth0Context.Consumer>
</Auth0Provider>
</Provider>
);
};