Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixing auth context refresh #529

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading