From 93ac402c3c2cd9ed301d09d123c6c9384a07f8cf Mon Sep 17 00:00:00 2001 From: Pierre Vasseur Date: Mon, 14 Oct 2024 14:09:04 +0200 Subject: [PATCH 1/2] add interval to renew token --- .prettierignore | 1 + .../auth/open-id-connect-auth/use-oidc.tsx | 24 +++++++++++-------- src/vite-env.d.ts | 5 ++-- 3 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 000000000..c8e861dd3 --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +src/vite-env.d.ts \ No newline at end of file diff --git a/src/packages/auth/open-id-connect-auth/use-oidc.tsx b/src/packages/auth/open-id-connect-auth/use-oidc.tsx index bce58240b..fc3b53bd3 100644 --- a/src/packages/auth/open-id-connect-auth/use-oidc.tsx +++ b/src/packages/auth/open-id-connect-auth/use-oidc.tsx @@ -17,7 +17,15 @@ const LoginOidcComponent = ({ const { isUserLoggedIn, login, oidcTokens } = useOidc({ assertUserLoggedIn: false, }); - const [userInformationsLoaded, setUserInformationsLoaded] = useState(false); + const { renewTokens } = useOidc({ assertUserLoggedIn: true }); + const [userInformationLoaded, setUserInformationLoaded] = useState(false); + + if (!isUserLoggedIn) { + login({ + doesCurrentHrefRequiresAuth: true, + }); + return null; + } useEffect(() => { if (isUserLoggedIn) { @@ -25,19 +33,15 @@ const LoginOidcComponent = ({ UsersApi.getStamp().then(({ stamp }: { stamp: string }) => { const roles = (oidcTokens?.decodedIdToken.realm_access as any).roles; saveUserProps({ roles, stamp }); - setUserInformationsLoaded(true); + setUserInformationLoaded(true); }); } + setInterval(() => { + renewTokens(); + }, 120000); }, []); - if (!isUserLoggedIn) { - login({ - doesCurrentHrefRequiresAuth: true, - }); - return null; - } - - if (!userInformationsLoaded) { + if (!userInformationLoaded) { return null; } diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index e1710d541..cfac9520c 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -11,10 +11,9 @@ type ImportMetaEnv = { VITE_OIDC_CLIENT_ID: string VITE_CONCEPTS_DOCUMENTATION: string VITE_OPERATIONS_DOCUMENTATION: string - VITE_VERSION: string - VITE_NAME: string - VITE_INSEE: string VITE_DEV_TOOLS_ENABLED: string + VITE_NAME: string + VITE_VERSION: string BASE_URL: string MODE: string DEV: boolean From 927fd91d0b5c0a867fe90b863fb3f3af47527728 Mon Sep 17 00:00:00 2001 From: Pierre Vasseur Date: Mon, 14 Oct 2024 14:14:39 +0200 Subject: [PATCH 2/2] set types to env var --- src/vite-env.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index cfac9520c..9830c48ca 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -11,9 +11,10 @@ type ImportMetaEnv = { VITE_OIDC_CLIENT_ID: string VITE_CONCEPTS_DOCUMENTATION: string VITE_OPERATIONS_DOCUMENTATION: string - VITE_DEV_TOOLS_ENABLED: string + VITE_DEV_TOOLS_ENABLED: boolean VITE_NAME: string VITE_VERSION: string + VITE_INSEE: boolean BASE_URL: string MODE: string DEV: boolean