forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthBootstrap.tsx
104 lines (95 loc) · 2.8 KB
/
AuthBootstrap.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import React, { useContext, useEffect, useMemo, useState } from 'react';
import {
AuthConfigMap,
AuthPluginAnonymous,
AuthPluginParent,
AuthPluginPsk,
} from '@deephaven/auth-plugins';
import { LoadingOverlay } from '@deephaven/components';
import { useClient } from '@deephaven/jsapi-bootstrap';
import { getErrorMessage } from '@deephaven/utils';
import { PluginsContext } from '@deephaven/plugin';
import { getAuthPluginComponent } from '../plugins';
import LoginNotifier from './LoginNotifier';
export type AuthBootstrapProps = {
/**
* The children to render after authentication is completed.
*/
children: React.ReactNode;
};
/** Core auth plugins that are always loaded */
const CORE_AUTH_PLUGINS = new Map([
[
'@deephaven/auth-plugins.AuthPluginParent',
{ AuthPlugin: AuthPluginParent },
],
['@deephaven/auth-plugins.AuthPluginPsk', { AuthPlugin: AuthPluginPsk }],
[
'@deephaven/auth-plugins.AuthPluginAnonymous',
{ AuthPlugin: AuthPluginAnonymous },
],
]);
/**
* AuthBootstrap component. Handles displaying the auth plugin and authenticating.
*/
export function AuthBootstrap({ children }: AuthBootstrapProps): JSX.Element {
const client = useClient();
// `useContext` instead of `usePlugins` so that we don't have to wait for the plugins to load
// We want to load the auth config values in parallel with the plugins
const plugins = useContext(PluginsContext);
const [authConfig, setAuthConfig] = useState<AuthConfigMap>();
const [error, setError] = useState<unknown>();
useEffect(
function initAuthConfigValues() {
let isCanceled = false;
async function loadAuthConfigValues(): Promise<void> {
try {
const newAuthConfigValues = (await client.getAuthConfigValues()) as [
string,
string,
][];
if (!isCanceled) {
setAuthConfig(new Map(newAuthConfigValues));
}
} catch (e) {
if (!isCanceled) {
setError(e);
}
}
}
loadAuthConfigValues();
return () => {
isCanceled = true;
};
},
[client]
);
const AuthComponent = useMemo(() => {
if (plugins == null || authConfig == null) {
return undefined;
}
try {
return getAuthPluginComponent(plugins, authConfig, CORE_AUTH_PLUGINS);
} catch (e) {
setError(e);
}
}, [authConfig, plugins]);
const isLoading = AuthComponent == null || authConfig == null;
if (isLoading || error != null) {
return (
<LoadingOverlay
isLoading={isLoading && error == null}
errorMessage={getErrorMessage(error)}
/>
);
}
return (
<AuthComponent authConfigValues={authConfig}>
<>
<LoginNotifier />
{children}
</>
</AuthComponent>
);
}
export default AuthBootstrap;