Skip to content

Commit a67bcff

Browse files
committed
Refactor authentication service to use DirectCognitoAuthService
- Replaced CognitoAuthService with DirectCognitoAuthService in useAuthOperations and AuthProvider. - Updated all authentication-related methods to utilize the new DirectCognitoAuthService. - Removed CognitoAuthService and its associated tests. - Implemented DirectCognitoAuthService to handle authentication directly using AWS SDK. - Adjusted S3StorageService and BedrockService to fetch auth session using DirectCognitoAuthService. - Updated OAuthRedirectHandler to use DirectCognitoAuthService for session and user retrieval.
1 parent 8513760 commit a67bcff

17 files changed

+4476
-11338
lines changed

frontend/package-lock.json

Lines changed: 3893 additions & 10648 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@
3939
]
4040
},
4141
"dependencies": {
42-
"@aws-amplify/auth": "^6.12.4",
43-
"@aws-amplify/core": "^6.11.4",
44-
"@aws-amplify/ui-react": "^6.11.1",
4542
"@aws-sdk/client-bedrock-runtime": "^3.799.0",
43+
"@aws-sdk/client-cognito-identity-provider": "^3.799.0",
4644
"@aws-sdk/client-s3": "^3.802.0",
4745
"@aws-sdk/s3-request-presigner": "^3.802.0",
4846
"@capacitor/android": "^7.2.0",

frontend/src/common/api/__tests__/reportService.test.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ vi.mock('axios', () => ({
2222
},
2323
}));
2424

25-
// Mock auth
26-
vi.mock('@aws-amplify/auth', () => ({
27-
fetchAuthSession: vi.fn().mockResolvedValue({
28-
tokens: {
29-
idToken: {
30-
toString: () => 'mock-id-token',
31-
},
32-
},
33-
}),
34-
}));
35-
3625
// Mock dynamic imports to handle the service functions
3726
vi.mock('../reportService', async (importOriginal) => {
3827
const actual = (await importOriginal()) as typeof ReportServiceModule;

frontend/src/common/api/reportService.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios, { AxiosProgressEvent } from 'axios';
22
import { MedicalReport } from '../models/medicalReport';
3-
import { fetchAuthSession } from '@aws-amplify/auth';
3+
import { DirectCognitoAuthService } from '../services/auth/direct-cognito-auth-service';
4+
45
// Get the API URL from environment variables
56
const API_URL = import.meta.env.VITE_BASE_URL_API || '';
67

@@ -21,8 +22,10 @@ export const getAuthConfig = async (
2122
signal?: AbortSignal;
2223
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
2324
}> => {
24-
const session = await fetchAuthSession();
25-
const idToken = session.tokens?.idToken?.toString() || '';
25+
// Get tokens from DirectCognitoAuthService
26+
const tokens = DirectCognitoAuthService.getTokens();
27+
const idToken = tokens?.id_token || '';
28+
2629
return {
2730
headers: {
2831
Accept: 'application/json',

frontend/src/common/api/useGetCurrentUser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useQuery } from '@tanstack/react-query';
22

33
import { CognitoUser } from 'common/models/user';
44
import { QueryKey } from 'common/utils/constants';
5-
import CognitoAuthService from 'common/services/auth/cognito-auth-service';
5+
import { DirectCognitoAuthService } from 'common/services/auth/direct-cognito-auth-service';
66
import { mapCognitoUserToAppUser } from 'common/utils/user-mapper';
77

88
/**
@@ -17,7 +17,7 @@ export const useGetCurrentUser = () => {
1717
const getCurentUser = async (): Promise<CognitoUser> => {
1818
try {
1919
// Get current user from Cognito
20-
const cognitoUser = await CognitoAuthService.getCurrentUser();
20+
const cognitoUser = await DirectCognitoAuthService.getCurrentUser();
2121

2222
if (!cognitoUser) {
2323
throw new Error('User not found');

frontend/src/common/api/useGetUserTokens.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useQuery } from '@tanstack/react-query';
22

33
import { UserTokens } from 'common/models/auth';
44
import { QueryKey } from 'common/utils/constants';
5-
import CognitoAuthService from 'common/services/auth/cognito-auth-service';
5+
import { DirectCognitoAuthService } from 'common/services/auth/direct-cognito-auth-service';
66

77
/**
88
* An API hook which fetches tokens from AWS Cognito.
@@ -11,8 +11,8 @@ import CognitoAuthService from 'common/services/auth/cognito-auth-service';
1111
export const useGetUserTokens = () => {
1212
const getUserTokens = async (): Promise<UserTokens> => {
1313
try {
14-
// Get tokens from Cognito
15-
const tokens = await CognitoAuthService.getUserTokens();
14+
// Get tokens directly from DirectCognitoAuthService
15+
const tokens = DirectCognitoAuthService.getTokens();
1616

1717
if (!tokens) {
1818
throw new Error('Tokens not found.');

frontend/src/common/config/aws-config.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,38 +41,3 @@ export const COGNITO_CONFIG = {
4141
// Social providers
4242
SOCIAL_PROVIDERS: ['Google', 'SignInWithApple'],
4343
};
44-
45-
/**
46-
* Get redirect URLs for OAuth
47-
*/
48-
const redirectUrls = {
49-
signIn: [APP_CONFIG.hostedRedirectSignIn],
50-
signOut: [APP_CONFIG.hostedRedirectSignOut],
51-
};
52-
53-
/**
54-
* Amplify Configuration object for initializing Amplify (V6 format)
55-
*/
56-
export const amplifyConfig = {
57-
Auth: {
58-
Cognito: {
59-
userPoolId: COGNITO_CONFIG.USER_POOL_ID,
60-
userPoolClientId: COGNITO_CONFIG.USER_POOL_WEB_CLIENT_ID,
61-
identityPoolId: COGNITO_CONFIG.IDENTITY_POOL_ID,
62-
loginWith: {
63-
email: true,
64-
phone: false,
65-
username: false,
66-
oauth: {
67-
domain: COGNITO_CONFIG.OAUTH_DOMAIN,
68-
scopes: COGNITO_CONFIG.OAUTH_SCOPES,
69-
redirectSignIn: redirectUrls.signIn,
70-
redirectSignOut: redirectUrls.signOut,
71-
responseType: 'code' as const,
72-
},
73-
},
74-
},
75-
},
76-
// Configure only the region you need to use
77-
region: REGION,
78-
};

0 commit comments

Comments
 (0)