Skip to content

Commit 421d355

Browse files
committed
Add auth token to the frontend
1 parent cd263ac commit 421d355

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

frontend/src/common/api/reportService.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios, { AxiosProgressEvent, AxiosRequestConfig } from 'axios';
22
import { MedicalReport, ReportStatus, ReportCategory } from '../models/medicalReport';
3-
3+
import { fetchAuthSession } from '@aws-amplify/auth';
44
// Get the API URL from environment variables
55
const API_URL = import.meta.env.VITE_BASE_URL_API || '';
66

@@ -15,6 +15,18 @@ const mockReports: MedicalReport[] = [
1515
}
1616
];
1717

18+
/**
19+
* Creates an authenticated request config with bearer token
20+
*/
21+
const getAuthConfig = async (): Promise<AxiosRequestConfig> => {
22+
const session = await fetchAuthSession();
23+
return {
24+
headers: {
25+
Authorization: session.tokens?.idToken ? `Bearer ${session.tokens.idToken.toString()}` : ''
26+
}
27+
};
28+
};
29+
1830
/**
1931
* Error thrown when report operations fail.
2032
*/
@@ -58,32 +70,32 @@ export const uploadReport = async (
5870
// Create form data for file upload
5971
const formData = new FormData();
6072
formData.append('file', file);
61-
73+
6274
// Optional metadata about the file
6375
formData.append('fileName', file.name);
6476
formData.append('fileType', file.type);
6577
formData.append('fileSize', file.size.toString());
66-
78+
6779
// Setup request config with progress tracking if callback provided
6880
const config: AxiosRequestConfig = {
6981
headers: {
7082
'Content-Type': 'multipart/form-data'
7183
}
7284
};
73-
85+
7486
if (onProgress) {
7587
config.onUploadProgress = (progressEvent: AxiosProgressEvent) => {
76-
const percentCompleted = progressEvent.total
88+
const percentCompleted = progressEvent.total
7789
? Math.round((progressEvent.loaded * 100) / progressEvent.total) / 100
7890
: 0;
7991
onProgress(percentCompleted);
8092
};
8193
}
82-
94+
8395
// In a real app, this would be an actual API call
8496
// const response = await axios.post('/api/reports/upload', formData, config);
8597
// return response.data;
86-
98+
8799
// For demonstration purposes, simulate upload delay and return mock data
88100
await new Promise<void>(resolve => {
89101
// Simulate progress updates
@@ -97,7 +109,7 @@ export const uploadReport = async (
97109
}
98110
onProgress(progress);
99111
}, 200);
100-
112+
101113
// Resolve after simulated upload time
102114
setTimeout(() => {
103115
clearInterval(interval);
@@ -109,7 +121,7 @@ export const uploadReport = async (
109121
setTimeout(resolve, 2000);
110122
}
111123
});
112-
124+
113125
// Create a new report based on the uploaded file
114126
const newReport: MedicalReport = {
115127
id: String(mockReports.length + 1),
@@ -119,10 +131,10 @@ export const uploadReport = async (
119131
status: ReportStatus.UNREAD,
120132
documentUrl: `https://example.com/reports/${mockReports.length + 1}/${file.name}` // Mock URL
121133
};
122-
134+
123135
// Add to mock data
124136
mockReports.unshift(newReport);
125-
137+
126138
return newReport;
127139
} catch (error) {
128140
if (axios.isAxiosError(error)) {
@@ -139,8 +151,8 @@ export const uploadReport = async (
139151
*/
140152
const determineCategory = (filename: string): ReportCategory => {
141153
const lowerFilename = filename.toLowerCase();
142-
143-
const matchedCategory = Object.entries(CATEGORY_KEYWORDS).find(([_, keywords]) =>
154+
155+
const matchedCategory = Object.entries(CATEGORY_KEYWORDS).find(([_, keywords]) =>
144156
keywords.some(keyword => lowerFilename.includes(keyword))
145157
);
146158

@@ -154,7 +166,7 @@ const determineCategory = (filename: string): ReportCategory => {
154166
*/
155167
export const fetchLatestReports = async (limit = 3): Promise<MedicalReport[]> => {
156168
try {
157-
const response = await axios.get(`${API_URL}/api/reports/latest?limit=${limit}`);
169+
const response = await axios.get(`${API_URL}/api/reports/latest?limit=${limit}`, await getAuthConfig());
158170
console.log('response', response.data);
159171
console.log('API_URL', API_URL);
160172
return response.data;
@@ -172,7 +184,7 @@ export const fetchLatestReports = async (limit = 3): Promise<MedicalReport[]> =>
172184
*/
173185
export const fetchAllReports = async (): Promise<MedicalReport[]> => {
174186
try {
175-
const response = await axios.get(`${API_URL}/api/reports`);
187+
const response = await axios.get(`${API_URL}/api/reports`, await getAuthConfig() );
176188
return response.data;
177189
} catch (error) {
178190
if (axios.isAxiosError(error)) {

0 commit comments

Comments
 (0)