-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAuthProvider.tsx
47 lines (41 loc) · 1.82 KB
/
AuthProvider.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
// SPDX-FileCopyrightText: 2024 German Aerospace Center (DLR) and CISPA Helmholtz Center for Information Security
// SPDX-License-Identifier: Apache-2.0
import React from 'react';
import {ReactNode} from 'react';
import {AuthProvider as OAuth2WithPkceProvider, TAuthConfig} from 'react-oauth2-code-pkce';
import {useAppSelector} from 'store/hooks';
interface AuthProviderProps {
children: ReactNode;
}
function AuthProvider({children}: AuthProviderProps) {
const realm = useAppSelector((state) => state.realm.name);
let authConfig: TAuthConfig;
if (!import.meta.env.VITE_OAUTH_CLIENT_ID || !import.meta.env.VITE_OAUTH_API_URL) {
// in case required auth env vars are not set
console.warn(
'Missing environment variables: VITE_OAUTH_CLIENT_ID or VITE_OAUTH_API_URL. Please set it to enable authentication.'
);
authConfig = {
clientId: 'client-placeholder',
authorizationEndpoint: 'auth-endpoint-placeholder',
tokenEndpoint: 'token-endpoint-placeholder',
redirectUri: 'redirect-uri-placeholder',
autoLogin: false,
};
} else {
// actual auth configurations
authConfig = {
clientId: `${import.meta.env.VITE_OAUTH_CLIENT_ID}`,
authorizationEndpoint: `${import.meta.env.VITE_OAUTH_API_URL}/realms/${realm}/protocol/openid-connect/auth`,
tokenEndpoint: `${import.meta.env.VITE_OAUTH_API_URL}/realms/${realm}/protocol/openid-connect/token`,
redirectUri:
import.meta.env.VITE_OAUTH_REDIRECT_URL === undefined
? window.location.origin
: `${import.meta.env.VITE_OAUTH_REDIRECT_URL}`,
scope: 'openid profile email loki-back-audience roles', // default scope without audience
autoLogin: false,
};
}
return <OAuth2WithPkceProvider authConfig={authConfig}>{children}</OAuth2WithPkceProvider>;
}
export default AuthProvider;