Skip to content

Commit

Permalink
fix: fixing auth context refresh (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj authored Dec 6, 2024
1 parent da631f2 commit e0e1483
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
12 changes: 3 additions & 9 deletions frontend/src/__test__/services/AuthService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
Expand All @@ -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',
Expand All @@ -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',
};
Expand All @@ -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',
Expand All @@ -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',
};
Expand All @@ -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',
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/contexts/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -53,7 +53,7 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {

useEffect(() => {
refreshUserState();
const interval = setInterval(refreshUserState, 3 * 60 * 1000);
const interval = setInterval(loadUserToken, 3 * 60 * 1000);
return () => clearInterval(interval);
}, []);

Expand Down Expand Up @@ -105,6 +105,7 @@ export const useGetAuth = (): AuthContextType => {
const loadUserToken = async () : Promise<JWT|undefined> => {
if(env.NODE_ENV !== 'test'){
const { idToken } = (await fetchAuthSession()).tokens ?? {};
setAuthIdToken(idToken?.toString() || null);
return idToken;
} else {
// This is for test only
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/services/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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}`
Expand Down

0 comments on commit e0e1483

Please sign in to comment.