Skip to content

Commit

Permalink
fix: authentication header in fetch requests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoBaroso committed Jun 3, 2024
1 parent 1b7e588 commit a7d2ba7
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 31 deletions.
31 changes: 14 additions & 17 deletions frontend/src/services/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -18,7 +17,7 @@ const api = {
export default api;

type HttpRequestOptions = {
authRequired?: boolean;
authToken?: string;
body?: object;
};

Expand All @@ -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 = {
Expand All @@ -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) {
Expand All @@ -95,13 +94,11 @@ export class Api {
}

static async buildHeaders(
authRequired: boolean = false,
token: string = "",
json: boolean = false
): Promise<HeadersInit> {
const headers: HeadersInit = new Headers();
if (authRequired) {
const { getAccessTokenSilently } = useAuth0();
const token = await getAccessTokenSilently();
if (token) {
headers.append("Authorization", `Bearer ${token}`);
}
if (json) {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/services/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/services/availabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};

Expand Down
16 changes: 12 additions & 4 deletions frontend/src/services/interviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/services/recruitmentSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/services/timeslots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/services/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};

Expand Down

0 comments on commit a7d2ba7

Please sign in to comment.