From e5b511148a39cf5ebb6bc958121c5218011b40b7 Mon Sep 17 00:00:00 2001 From: Ojakoo Date: Sun, 12 Feb 2023 12:52:48 +0200 Subject: [PATCH] add authentication wrapper to api requests --- src/api/auth.ts | 41 ++++++++++++++++++++++++++++++++++++++++- src/api/eventApi.ts | 21 ++++++++++++--------- src/api/feedApi.ts | 21 ++++++++++++--------- src/api/jobAdApi.ts | 21 ++++++++++++--------- src/api/signupApi.ts | 33 ++++++++++++++++++--------------- 5 files changed, 94 insertions(+), 43 deletions(-) diff --git a/src/api/auth.ts b/src/api/auth.ts index bda066c..a4c4463 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -1,7 +1,10 @@ import { deleteTokenCookies, getAccessTokenCookie, getRefreshTokenCookie, setAccessTokenCookie, setRefreshTokenCookie, } from "@utils/auth"; -import { APIPath, postBackendAPI } from "./backend"; +import { + APIPath, postBackendAPI, getBackendAPI, putBackendAPI, deleteBackendAPI, + API, +} from "./backend"; export type AuthTokenRequest = { username: string; @@ -71,3 +74,39 @@ export const authenticate = async (): Promise => { return refreshToken(); } }; + +export const authedGetBackendAPI = async ({ + path, urlParams, queryParams, authenticated = true, +}: API): Promise => { + if (authenticated) await authenticate(); + return getBackendAPI({ + path, urlParams, queryParams, authenticated, + }); +}; + +export const authedPostBackendAPI = async ({ + path, urlParams, queryParams, authenticated = true, +}: API, body: RequestType): Promise => { + if (authenticated) await authenticate(); + return postBackendAPI({ + path, urlParams, queryParams, authenticated, + }, body); +}; + +export const authedPutBackendAPI = async ({ + path, urlParams, queryParams, authenticated = true, +}: API, body: RequestType): Promise => { + if (authenticated) await authenticate(); + return putBackendAPI({ + path, urlParams, queryParams, authenticated, + }, body); +}; + +export const authedDeleteBackendAPI = async ({ + path, urlParams, queryParams, authenticated = true, +}: API): Promise => { + if (authenticated) await authenticate(); + return deleteBackendAPI({ + path, urlParams, queryParams, authenticated, + }); +}; diff --git a/src/api/eventApi.ts b/src/api/eventApi.ts index 13c978f..3d726a9 100644 --- a/src/api/eventApi.ts +++ b/src/api/eventApi.ts @@ -1,8 +1,11 @@ /* eslint-disable no-console */ import Event from "@models/Event"; import { - APIPath, deleteBackendAPI, getBackendAPI, postBackendAPI, putBackendAPI, + APIPath, } from "./backend"; +import { + authedGetBackendAPI, authedPostBackendAPI, authedDeleteBackendAPI, authedPutBackendAPI, +} from "./auth"; interface Options { limit?: number; @@ -14,7 +17,7 @@ interface Options { class EventApi { static getEvent = async (id: number, auth = false): Promise => { try { - return await getBackendAPI({ + return await authedGetBackendAPI({ path: APIPath.EVENTS, urlParams: { id }, authenticated: auth, }); } catch (err) { @@ -24,10 +27,10 @@ class EventApi { }; static getEvents = async ({ - since, limit, offset, auth, + since, limit, offset, auth = false, }: Options = {}): Promise => { try { - return await getBackendAPI({ + return await authedGetBackendAPI({ path: APIPath.EVENTS, queryParams: { since, @@ -44,8 +47,8 @@ class EventApi { static createEvent = async (data: Event): Promise => { try { - return await postBackendAPI({ - path: APIPath.EVENTS, authenticated: true, + return await authedPostBackendAPI({ + path: APIPath.EVENTS, }, data); } catch (err) { console.error(err); @@ -55,8 +58,8 @@ class EventApi { static updateEvent = async (data: Event): Promise => { try { - return await putBackendAPI({ - path: APIPath.EVENTS, urlParams: { id: data.id }, authenticated: true, + return await authedPutBackendAPI({ + path: APIPath.EVENTS, urlParams: { id: data.id }, }, data); } catch (err) { console.error(err); @@ -66,7 +69,7 @@ class EventApi { static deleteEvent = async (id: number): Promise => { try { - await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id }, authenticated: true }); + await authedDeleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id } }); } catch (err) { console.error(err); throw err; diff --git a/src/api/feedApi.ts b/src/api/feedApi.ts index e198b47..acd917e 100644 --- a/src/api/feedApi.ts +++ b/src/api/feedApi.ts @@ -1,8 +1,11 @@ /* eslint-disable no-console */ import Post from "@models/Feed"; import { - APIPath, deleteBackendAPI, getBackendAPI, postBackendAPI, putBackendAPI, + APIPath, } from "./backend"; +import { + authedGetBackendAPI, authedPostBackendAPI, authedDeleteBackendAPI, authedPutBackendAPI, +} from "./auth"; interface Options { limit?: number; @@ -11,9 +14,9 @@ interface Options { } class FeedApi { - static getPost = async (id: number, auth?: boolean): Promise => { + static getPost = async (id: number, auth = false): Promise => { try { - return await getBackendAPI({ + return await authedGetBackendAPI({ path: APIPath.FEED, urlParams: { id }, authenticated: auth, }); } catch (err) { @@ -22,9 +25,9 @@ class FeedApi { } }; - static getFeed = async ({ limit, offset, auth }: Options = {}): Promise => { + static getFeed = async ({ limit, offset, auth = false }: Options = {}): Promise => { try { - return await getBackendAPI({ + return await authedGetBackendAPI({ path: APIPath.FEED, queryParams: { limit, @@ -40,7 +43,7 @@ class FeedApi { static createPost = async (data: Post): Promise => { try { - return await postBackendAPI({ path: APIPath.FEED, authenticated: true }, data); + return await authedPostBackendAPI({ path: APIPath.FEED }, data); } catch (err) { console.error(err); throw err; @@ -49,8 +52,8 @@ class FeedApi { static updatePost = async (data: Post): Promise => { try { - return await putBackendAPI({ - path: APIPath.FEED, urlParams: { id: data.id }, authenticated: true, + return await authedPutBackendAPI({ + path: APIPath.FEED, urlParams: { id: data.id }, }, data); } catch (err) { console.error(err); @@ -60,7 +63,7 @@ class FeedApi { static deletePost = async (id: number): Promise => { try { - await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id }, authenticated: true }); + await authedDeleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id } }); } catch (err) { console.error(err); throw err; diff --git a/src/api/jobAdApi.ts b/src/api/jobAdApi.ts index 4b74db5..eaacdbd 100644 --- a/src/api/jobAdApi.ts +++ b/src/api/jobAdApi.ts @@ -1,8 +1,11 @@ /* eslint-disable no-console */ import JobAd from "@models/JobAd"; import { - APIPath, deleteBackendAPI, getBackendAPI, postBackendAPI, putBackendAPI, + APIPath, } from "./backend"; +import { + authedGetBackendAPI, authedPostBackendAPI, authedDeleteBackendAPI, authedPutBackendAPI, +} from "./auth"; interface Options { since?: Date; @@ -14,7 +17,7 @@ interface Options { class JobAdApi { static getJobAd = async (id: number, auth = false): Promise => { try { - return await getBackendAPI({ + return await authedGetBackendAPI({ path: APIPath.JOBADS, urlParams: { id }, authenticated: auth, }); } catch (err) { @@ -24,10 +27,10 @@ class JobAdApi { }; static getJobAds = async ({ - since, limit, offset, auth, + since, limit, offset, auth = false, }: Options = {}): Promise => { try { - return await getBackendAPI({ + return await authedGetBackendAPI({ path: APIPath.JOBADS, queryParams: { since, @@ -44,8 +47,8 @@ class JobAdApi { static createJobAd = async (data: JobAd): Promise => { try { - return await postBackendAPI({ - path: APIPath.JOBADS, authenticated: true, + return await authedPostBackendAPI({ + path: APIPath.JOBADS, }, data); } catch (err) { console.error(err); @@ -55,8 +58,8 @@ class JobAdApi { static updateJobAd = async (data: JobAd): Promise => { try { - return await putBackendAPI({ - path: APIPath.JOBADS, urlParams: { id: data.id }, authenticated: true, + return await authedPutBackendAPI({ + path: APIPath.JOBADS, urlParams: { id: data.id }, }, data); } catch (err) { console.error(err); @@ -66,7 +69,7 @@ class JobAdApi { static deleteJobAd = async (id: number): Promise => { try { - await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.JOBADS, urlParams: { id }, authenticated: true }); + await authedDeleteBackendAPI<{ message: "OK" }>({ path: APIPath.JOBADS, urlParams: { id } }); } catch (err) { console.error(err); throw err; diff --git a/src/api/signupApi.ts b/src/api/signupApi.ts index 467ca23..27623ba 100644 --- a/src/api/signupApi.ts +++ b/src/api/signupApi.ts @@ -1,8 +1,11 @@ /* eslint-disable no-console */ import { Signup, SignupForm } from "@models/Signup"; import { - APIPath, deleteBackendAPI, getBackendAPI, postBackendAPI, putBackendAPI, + APIPath, postBackendAPI, } from "./backend"; +import { + authedGetBackendAPI, authedPostBackendAPI, authedDeleteBackendAPI, authedPutBackendAPI, +} from "./auth"; export type EmailRequest = { mode: "all" | "actual" | "reserve"; @@ -13,8 +16,8 @@ export type EmailRequest = { class SignupApi { static getSignup = async (id: number): Promise => { try { - return await getBackendAPI({ - path: APIPath.SIGNUPS, urlParams: { id }, authenticated: true, + return await authedGetBackendAPI({ + path: APIPath.SIGNUPS, urlParams: { id }, }); } catch (err) { console.error(err); @@ -37,7 +40,7 @@ class SignupApi { try { const { id } = data; if (!id) throw new Error("SignupId required!"); - return await putBackendAPI({ + return await authedPutBackendAPI({ path: APIPath.SIGNUPS_EDIT, urlParams: { id, @@ -54,7 +57,7 @@ class SignupApi { static getSignupUUID = async (id: number, uuid: string): Promise => { try { - return await getBackendAPI({ + return await authedGetBackendAPI({ path: APIPath.SIGNUPS_EDIT, urlParams: { id, @@ -71,7 +74,7 @@ class SignupApi { static deleteSignup = async (id: number): Promise => { try { - await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.SIGNUPS, urlParams: { id }, authenticated: true }); + await authedDeleteBackendAPI<{ message: "OK" }>({ path: APIPath.SIGNUPS, urlParams: { id } }); } catch (err) { console.error(err); throw err; @@ -80,7 +83,7 @@ class SignupApi { static getForm = async (id: number, auth = false): Promise => { try { - return await getBackendAPI({ + return await authedGetBackendAPI({ path: APIPath.SIGNUP_FORMS, urlParams: { id }, authenticated: auth, }); } catch (err) { @@ -91,7 +94,7 @@ class SignupApi { static getForms = async (auth = false): Promise => { try { - return await getBackendAPI({ + return await authedGetBackendAPI({ path: APIPath.SIGNUP_FORMS, authenticated: auth, }); } catch (err) { @@ -102,8 +105,8 @@ class SignupApi { static createForm = async (data: SignupForm): Promise => { try { - return await postBackendAPI({ - path: APIPath.SIGNUP_FORMS, authenticated: true, + return await authedPostBackendAPI({ + path: APIPath.SIGNUP_FORMS, }, data); } catch (err) { console.error(err); @@ -113,8 +116,8 @@ class SignupApi { static updateForm = async (data: SignupForm): Promise => { try { - return await putBackendAPI({ - path: APIPath.SIGNUP_FORMS, urlParams: { id: data.id }, authenticated: true, + return await authedPutBackendAPI({ + path: APIPath.SIGNUP_FORMS, urlParams: { id: data.id }, }, data); } catch (err) { console.error(err); @@ -124,7 +127,7 @@ class SignupApi { static deleteForm = async (id: number): Promise => { try { - await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.SIGNUP_FORMS, urlParams: { id }, authenticated: true }); + await authedDeleteBackendAPI<{ message: "OK" }>({ path: APIPath.SIGNUP_FORMS, urlParams: { id } }); } catch (err) { console.error(err); throw err; @@ -133,7 +136,7 @@ class SignupApi { static signupFormSendEmail = async (data: EmailRequest, id: number): Promise => { try { - await postBackendAPI({ path: APIPath.SIGNUP_FORMS_EMAIL, urlParams: { id }, authenticated: true }, data); + await authedPostBackendAPI({ path: APIPath.SIGNUP_FORMS_EMAIL, urlParams: { id } }, data); } catch (err) { console.error(err); throw err; @@ -142,7 +145,7 @@ class SignupApi { static getSignups = async (id: number): Promise => { try { - return await getBackendAPI({ path: APIPath.SIGNUP_FORMS_SIGNUPS, urlParams: { id }, authenticated: true }); + return await authedGetBackendAPI({ path: APIPath.SIGNUP_FORMS_SIGNUPS, urlParams: { id } }); } catch (err) { console.error(err); throw err;