From a7d2ba779a28148f9468c0028fd588f2f195313c Mon Sep 17 00:00:00 2001 From: Alberto Baroso Date: Mon, 3 Jun 2024 19:15:51 +0200 Subject: [PATCH] fix: authentication header in fetch requests --- frontend/src/services/API.ts | 31 +++++++++----------- frontend/src/services/applications.ts | 4 +-- frontend/src/services/availabilities.ts | 4 +-- frontend/src/services/interviews.ts | 16 +++++++--- frontend/src/services/recruitmentSessions.ts | 4 +-- frontend/src/services/timeslots.ts | 4 +-- frontend/src/services/users.ts | 4 +-- 7 files changed, 36 insertions(+), 31 deletions(-) diff --git a/frontend/src/services/API.ts b/frontend/src/services/API.ts index 6e54325..56052af 100644 --- a/frontend/src/services/API.ts +++ b/frontend/src/services/API.ts @@ -4,7 +4,6 @@ import timeSlots from "./timeSlots"; import interviews from "./interviews"; import availabilities from "./availabilities"; import recruitmentSessions from "./recruitmentSessions"; -import { useAuth0 } from "@auth0/auth0-react"; const api = { users, @@ -18,7 +17,7 @@ const api = { export default api; type HttpRequestOptions = { - authRequired?: boolean; + authToken?: string; body?: object; }; @@ -36,7 +35,7 @@ export class Api { try { /* Prepare request parameters */ const headers: HeadersInit = await Api.buildHeaders( - options.authRequired ?? false, + options.authToken ?? "", true ); const init = { @@ -54,36 +53,36 @@ export class Api { return await this.handleResponse(response); } - static async get(endpoint: string, authRequired: boolean = true) { - return Api.httpRequest(endpoint, "GET", { authRequired }); + static async get(endpoint: string, authToken: string = "") { + return Api.httpRequest(endpoint, "GET", { authToken }); } static async post( endpoint: string, body: object = {}, - authRequired: boolean = false + authToken: string = "" ) { - return Api.httpRequest(endpoint, "POST", { authRequired, body }); + return Api.httpRequest(endpoint, "POST", { authToken, body }); } static async put( endpoint: string, body: object = {}, - authRequired: boolean = false + authToken: string = "" ) { - return Api.httpRequest(endpoint, "PUT", { authRequired, body }); + return Api.httpRequest(endpoint, "PUT", { authToken, body }); } static async patch( endpoint: string, body: object = {}, - authRequired: boolean = false + authToken: string = "" ) { - return Api.httpRequest(endpoint, "PATCH", { authRequired, body }); + return Api.httpRequest(endpoint, "PATCH", { authToken, body }); } - static async delete(endpoint: string, authRequired: boolean = false) { - return Api.httpRequest(endpoint, "DELETE", { authRequired }); + static async delete(endpoint: string, authToken: string = "") { + return Api.httpRequest(endpoint, "DELETE", { authToken }); } static async handleResponse(response: Response) { @@ -95,13 +94,11 @@ export class Api { } static async buildHeaders( - authRequired: boolean = false, + token: string = "", json: boolean = false ): Promise { const headers: HeadersInit = new Headers(); - if (authRequired) { - const { getAccessTokenSilently } = useAuth0(); - const token = await getAccessTokenSilently(); + if (token) { headers.append("Authorization", `Bearer ${token}`); } if (json) { diff --git a/frontend/src/services/applications.ts b/frontend/src/services/applications.ts index 9a11348..403bdd3 100644 --- a/frontend/src/services/applications.ts +++ b/frontend/src/services/applications.ts @@ -3,8 +3,8 @@ import { Api } from "./api"; const resource = "applications"; const applications = { - getApplications: async () => { - return await Api.get(`${resource}`); + getApplications: async (accessToken: string | undefined) => { + return await Api.get(`${resource}`, accessToken); }, }; diff --git a/frontend/src/services/availabilities.ts b/frontend/src/services/availabilities.ts index cb20476..0dd558b 100644 --- a/frontend/src/services/availabilities.ts +++ b/frontend/src/services/availabilities.ts @@ -3,8 +3,8 @@ import { Api } from "./api"; const resource = "availabilities"; const availabilities = { - getAvailabilities: async () => { - return await Api.get(`${resource}`); + getAvailabilities: async (accessToken: string | undefined) => { + return await Api.get(`${resource}`, accessToken); }, }; diff --git a/frontend/src/services/interviews.ts b/frontend/src/services/interviews.ts index 02613f8..660aef9 100644 --- a/frontend/src/services/interviews.ts +++ b/frontend/src/services/interviews.ts @@ -3,13 +3,21 @@ import { Api } from "./api"; const resource = "interviews"; const interviews = { - getInterviewsByDates: async (startDate: string, endDate: string) => { + getInterviewsByDates: async ( + startDate: string, + endDate: string, + accessToken: string | undefined + ) => { return await Api.get( - `${resource}?startDate=${startDate}&endDate=${endDate}}` + `${resource}?startDate=${startDate}&endDate=${endDate}}`, + accessToken ); }, - getInterviewsByDate: async (date: string) => { - return await Api.get(`${resource}?date=${date}`); + getInterviewsByDate: async ( + date: string, + accessToken: string | undefined + ) => { + return await Api.get(`${resource}?date=${date}`, accessToken); }, }; diff --git a/frontend/src/services/recruitmentSessions.ts b/frontend/src/services/recruitmentSessions.ts index 0a2b37e..20de7c4 100644 --- a/frontend/src/services/recruitmentSessions.ts +++ b/frontend/src/services/recruitmentSessions.ts @@ -3,8 +3,8 @@ import { Api } from "./api"; const resource = "recruitment-session"; const recruitmentSessions = { - getActive: async () => { - return await Api.get(`${resource}`); + getActive: async (accessToken: string | undefined) => { + return await Api.get(`${resource}`, accessToken); }, }; diff --git a/frontend/src/services/timeslots.ts b/frontend/src/services/timeslots.ts index 76d7140..59e3911 100644 --- a/frontend/src/services/timeslots.ts +++ b/frontend/src/services/timeslots.ts @@ -3,8 +3,8 @@ import { Api } from "./api"; const resource = "timeslots"; const timeslots = { - getApplicants: async () => { - return await Api.get(`${resource}`); + getApplicants: async (accessToken: string | undefined) => { + return await Api.get(`${resource}`, accessToken); }, }; diff --git a/frontend/src/services/users.ts b/frontend/src/services/users.ts index 31dfd32..1420654 100644 --- a/frontend/src/services/users.ts +++ b/frontend/src/services/users.ts @@ -3,8 +3,8 @@ import { Api } from "./api"; const resource = "users"; const users = { - getUsers: async () => { - return await Api.get(`${resource}`); + getUsers: async (accessToken: string | undefined) => { + return await Api.get(`${resource}`, accessToken); }, };