-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathSamlPage.tsx
49 lines (44 loc) · 1.47 KB
/
SamlPage.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
import {buildSamlClient, SamlClient, SamlClientOptions} from '@coveo/auth';
import {buildSearchEngine} from '@coveo/headless';
import {
useState,
useMemo,
useRef,
useEffect,
PropsWithChildren,
FunctionComponent,
} from 'react';
import {AppContext} from '../context/engine';
const samlClientOptions: SamlClientOptions = {
organizationId: '',
provider: '',
};
export const SamlPage: FunctionComponent<PropsWithChildren> = ({children}) => {
const [initialAccessToken, setInitialAccessToken] = useState('');
const samlClient = useRef<SamlClient | null>(null);
useEffect(() => {
if (samlClient.current) {
// `SamlClient.authenticate` is not idempotent. Calling it twice after redirection from the provider, even on different clients, will cause a redirection loop.
return;
}
samlClient.current = buildSamlClient(samlClientOptions);
samlClient.current.authenticate().then(setInitialAccessToken);
}, []);
const engine = useMemo(
() =>
initialAccessToken && samlClient.current
? buildSearchEngine({
configuration: {
organizationId: samlClientOptions.organizationId,
accessToken: initialAccessToken,
renewAccessToken: samlClient.current.authenticate,
},
})
: null,
[samlClientOptions, samlClient.current, initialAccessToken]
);
if (!engine) {
return null;
}
return <AppContext.Provider value={{engine}}>{children}</AppContext.Provider>;
};