1
- import React , { Suspense } from "react" ;
2
- import { ReactKeycloakProvider } from "@react-keycloak/web" ;
1
+ import { initInterceptors } from "@app/axios-config" ;
2
+ import { isAuthRequired } from "@app/Constants" ;
3
+ import i18n from "@app/i18n" ;
3
4
import keycloak from "@app/keycloak" ;
5
+ import { deleteCookie , getCookie , setCookie } from "@app/queries/cookies" ;
4
6
import { AppPlaceholder } from "./AppPlaceholder" ;
5
- import { initInterceptors } from "@app/axios-config" ;
7
+ import { Flex , FlexItem , Spinner } from "@patternfly/react-core" ;
8
+ import { ReactKeycloakProvider } from "@react-keycloak/web" ;
9
+ import React , { Suspense } from "react" ;
6
10
7
11
interface IKeycloakProviderProps {
8
12
children : React . ReactNode ;
@@ -11,17 +15,81 @@ interface IKeycloakProviderProps {
11
15
export const KeycloakProvider : React . FC < IKeycloakProviderProps > = ( {
12
16
children,
13
17
} ) => {
14
- React . useEffect ( ( ) => {
15
- initInterceptors ( ) ;
16
- } , [ ] ) ;
18
+ const checkAuthCookie = ( ) => {
19
+ if ( ! getCookie ( "keycloak_cookie" ) && keycloak ?. token ) {
20
+ setCookie ( "keycloak_cookie" , keycloak . token , 365 ) ;
21
+ }
22
+ } ;
23
+ if ( isAuthRequired ) {
24
+ return (
25
+ < >
26
+ < ReactKeycloakProvider
27
+ authClient = { keycloak }
28
+ initOptions = { { onLoad : "login-required" } }
29
+ LoadingComponent = {
30
+ < Flex
31
+ spaceItems = { { default : "spaceItemsSm" } }
32
+ alignItems = { { default : "alignItemsCenter" } }
33
+ flexWrap = { { default : "nowrap" } }
34
+ style = { {
35
+ width : "100%" ,
36
+ height : "100%" ,
37
+ } }
38
+ >
39
+ < FlexItem
40
+ style = { {
41
+ margin : "auto auto" ,
42
+ textAlign : "center" ,
43
+ } }
44
+ >
45
+ < Spinner > Loading...</ Spinner >
46
+ </ FlexItem >
47
+ </ Flex >
48
+ }
49
+ isLoadingCheck = { ( keycloak ) => {
50
+ if ( keycloak . authenticated ) {
51
+ initInterceptors (
52
+ ( ) =>
53
+ new Promise < string > ( ( resolve , reject ) => {
54
+ if ( keycloak . token ) {
55
+ if ( keycloak . refreshToken ) {
56
+ keycloak
57
+ . updateToken ( 60 )
58
+ . then ( ( ) => {
59
+ deleteCookie ( "keycloak_cookie" ) ;
60
+ checkAuthCookie ( ) ;
61
+ return resolve ( keycloak . token ! ) ;
62
+ } )
63
+ . catch ( ( err ) => {
64
+ console . log ( "err" , err ) ;
65
+ return reject ( "Failed to refresh token" ) ;
66
+ } ) ;
67
+ } else return resolve ( keycloak . token ! ) ;
68
+ } else {
69
+ keycloak . login ( ) ;
70
+ reject ( "Not logged in" ) ;
71
+ }
72
+ } )
73
+ ) ;
74
+
75
+ const kcLocale = ( keycloak . tokenParsed as any ) [ "locale" ] ;
76
+ if ( kcLocale ) {
77
+ i18n . changeLanguage ( kcLocale ) ;
78
+ }
79
+ }
17
80
18
- return (
19
- < ReactKeycloakProvider
20
- authClient = { keycloak }
21
- initOptions = { { onLoad : "login-required" } }
22
- LoadingComponent = { < AppPlaceholder /> }
23
- >
24
- < Suspense fallback = { < AppPlaceholder /> } > { children } </ Suspense >
25
- </ ReactKeycloakProvider >
26
- ) ;
81
+ return ! keycloak . authenticated ;
82
+ } }
83
+ >
84
+ { children }
85
+ </ ReactKeycloakProvider >
86
+ </ >
87
+ ) ;
88
+ } else {
89
+ return (
90
+ < >
91
+ < Suspense fallback = { < AppPlaceholder /> } > { children } </ Suspense >
92
+ </ >
93
+ ) ;
94
+ }
27
95
} ;
0 commit comments