From e0e14834820520541affabde8c94e07e29043ac8 Mon Sep 17 00:00:00 2001 From: Paulo Gomes da Cruz Junior Date: Fri, 6 Dec 2024 10:23:38 -0800 Subject: [PATCH] fix: fixing auth context refresh (#529) --- frontend/src/__test__/services/AuthService.test.ts | 12 +++--------- frontend/src/contexts/AuthProvider.tsx | 5 +++-- frontend/src/services/AuthService.ts | 3 +-- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/frontend/src/__test__/services/AuthService.test.ts b/frontend/src/__test__/services/AuthService.test.ts index 12c8b16c..9e1de0c9 100644 --- a/frontend/src/__test__/services/AuthService.test.ts +++ b/frontend/src/__test__/services/AuthService.test.ts @@ -34,8 +34,7 @@ describe('AuthService', () => { "custom:idp_name": "idir", "given_name": "Jack", "name": "Jack Ryan", - "family_name": "Ryan", - exp: 1234567890 + "family_name": "Ryan" }, toString: () => 'mockTokenString', }; @@ -51,7 +50,6 @@ describe('AuthService', () => { email: 'jack.ryan@gov.bc.ca', idpProvider: 'IDIR', clientRoles: [{ role: 'role1' }, { role: 'role2' }], - exp: 1234567890, firstName: 'Jack', lastName: 'Ryan', providerUsername: 'IDIR\\JRYAN', @@ -74,8 +72,7 @@ describe('AuthService', () => { "custom:idp_name": "idir", "given_name": "Jack", "name": "Jack Ryan", - "family_name": "Ryan", - exp: 1234567890 + "family_name": "Ryan" }, toString: () => 'mockTokenString', }; @@ -91,7 +88,6 @@ describe('AuthService', () => { email: 'jack.ryan@gov.bc.ca', idpProvider: 'IDIR', clientRoles: [{ role: 'role1' }, { role: 'role2' }], - exp: 1234567890, firstName: 'Jack', lastName: 'Ryan', providerUsername: 'IDIR\\JRYAN', @@ -103,8 +99,7 @@ describe('AuthService', () => { payload: { 'custom:idp_display_name': 'Doe, John', 'custom:idp_username': 'johndoe', - 'email': 'john.doe@example.com', - exp: 1234567890, + 'email': 'john.doe@example.com' }, toString: () => 'mockTokenString', }; @@ -120,7 +115,6 @@ describe('AuthService', () => { email: 'john.doe@example.com', idpProvider: '', clientRoles: [{ role: 'role1' }, { role: 'role2' }], - exp: 1234567890, firstName: 'John', lastName: 'Doe', providerUsername: '\\johndoe', diff --git a/frontend/src/contexts/AuthProvider.tsx b/frontend/src/contexts/AuthProvider.tsx index 426320fc..7f4921c0 100644 --- a/frontend/src/contexts/AuthProvider.tsx +++ b/frontend/src/contexts/AuthProvider.tsx @@ -1,6 +1,6 @@ import React, { createContext, useState, useContext, useEffect, useMemo, ReactNode } from 'react'; import { fetchAuthSession, signInWithRedirect, signOut } from "aws-amplify/auth"; -import { parseToken, FamLoginUser } from "../services/AuthService"; +import { parseToken, FamLoginUser, setAuthIdToken } from "../services/AuthService"; import { extractGroups } from '../utils/famUtils'; import { env } from '../env'; import { JWT } from '../types/amplify'; @@ -53,7 +53,7 @@ export const AuthProvider: React.FC = ({ children }) => { useEffect(() => { refreshUserState(); - const interval = setInterval(refreshUserState, 3 * 60 * 1000); + const interval = setInterval(loadUserToken, 3 * 60 * 1000); return () => clearInterval(interval); }, []); @@ -105,6 +105,7 @@ export const useGetAuth = (): AuthContextType => { const loadUserToken = async () : Promise => { if(env.NODE_ENV !== 'test'){ const { idToken } = (await fetchAuthSession()).tokens ?? {}; + setAuthIdToken(idToken?.toString() || null); return idToken; } else { // This is for test only diff --git a/frontend/src/services/AuthService.ts b/frontend/src/services/AuthService.ts index c97ed2dc..d981f06e 100644 --- a/frontend/src/services/AuthService.ts +++ b/frontend/src/services/AuthService.ts @@ -21,7 +21,7 @@ export interface FamLoginUser { } // Function to set the authIdToken variable -const setAuthIdToken = (token: string | null) => { +export const setAuthIdToken = (token: string | null) => { authIdToken = token; }; @@ -71,7 +71,6 @@ export const parseToken = (idToken: JWT | undefined): FamLoginUser | undefined = email, idpProvider, clientRoles: rolesArray, - exp: idToken?.payload.exp, firstName: sanitizedFirstName, lastName, providerUsername: `${idpProvider}\\${userName}`