Compare commits

...

1 Commits

Author SHA1 Message Date
Ojakoo e5b511148a add authentication wrapper to api requests 2023-02-12 12:52:48 +02:00
5 changed files with 94 additions and 43 deletions
+40 -1
View File
@@ -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<boolean> => {
return refreshToken();
}
};
export const authedGetBackendAPI = async <ResponseType>({
path, urlParams, queryParams, authenticated = true,
}: API): Promise<ResponseType> => {
if (authenticated) await authenticate();
return getBackendAPI<ResponseType>({
path, urlParams, queryParams, authenticated,
});
};
export const authedPostBackendAPI = async <RequestType, ResponseType>({
path, urlParams, queryParams, authenticated = true,
}: API, body: RequestType): Promise<ResponseType> => {
if (authenticated) await authenticate();
return postBackendAPI<RequestType, ResponseType>({
path, urlParams, queryParams, authenticated,
}, body);
};
export const authedPutBackendAPI = async <RequestType, ResponseType>({
path, urlParams, queryParams, authenticated = true,
}: API, body: RequestType): Promise<ResponseType> => {
if (authenticated) await authenticate();
return putBackendAPI<RequestType, ResponseType>({
path, urlParams, queryParams, authenticated,
}, body);
};
export const authedDeleteBackendAPI = async <ResponseType>({
path, urlParams, queryParams, authenticated = true,
}: API): Promise<ResponseType> => {
if (authenticated) await authenticate();
return deleteBackendAPI<ResponseType>({
path, urlParams, queryParams, authenticated,
});
};
+12 -9
View File
@@ -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<Event> => {
try {
return await getBackendAPI<Event>({
return await authedGetBackendAPI<Event>({
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<Event[]> => {
try {
return await getBackendAPI<Event[]>({
return await authedGetBackendAPI<Event[]>({
path: APIPath.EVENTS,
queryParams: {
since,
@@ -44,8 +47,8 @@ class EventApi {
static createEvent = async (data: Event): Promise<Event> => {
try {
return await postBackendAPI<Event, Event>({
path: APIPath.EVENTS, authenticated: true,
return await authedPostBackendAPI<Event, Event>({
path: APIPath.EVENTS,
}, data);
} catch (err) {
console.error(err);
@@ -55,8 +58,8 @@ class EventApi {
static updateEvent = async (data: Event): Promise<Event> => {
try {
return await putBackendAPI<Event, Event>({
path: APIPath.EVENTS, urlParams: { id: data.id }, authenticated: true,
return await authedPutBackendAPI<Event, Event>({
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<void> => {
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;
+12 -9
View File
@@ -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<Post> => {
static getPost = async (id: number, auth = false): Promise<Post> => {
try {
return await getBackendAPI<Post>({
return await authedGetBackendAPI<Post>({
path: APIPath.FEED, urlParams: { id }, authenticated: auth,
});
} catch (err) {
@@ -22,9 +25,9 @@ class FeedApi {
}
};
static getFeed = async ({ limit, offset, auth }: Options = {}): Promise<Post[]> => {
static getFeed = async ({ limit, offset, auth = false }: Options = {}): Promise<Post[]> => {
try {
return await getBackendAPI<Post[]>({
return await authedGetBackendAPI<Post[]>({
path: APIPath.FEED,
queryParams: {
limit,
@@ -40,7 +43,7 @@ class FeedApi {
static createPost = async (data: Post): Promise<Post> => {
try {
return await postBackendAPI<Post, Post>({ path: APIPath.FEED, authenticated: true }, data);
return await authedPostBackendAPI<Post, Post>({ path: APIPath.FEED }, data);
} catch (err) {
console.error(err);
throw err;
@@ -49,8 +52,8 @@ class FeedApi {
static updatePost = async (data: Post): Promise<Post> => {
try {
return await putBackendAPI<Post, Post>({
path: APIPath.FEED, urlParams: { id: data.id }, authenticated: true,
return await authedPutBackendAPI<Post, Post>({
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<void> => {
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;
+12 -9
View File
@@ -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<JobAd> => {
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<JobAd[]> => {
try {
return await getBackendAPI<JobAd[]>({
return await authedGetBackendAPI<JobAd[]>({
path: APIPath.JOBADS,
queryParams: {
since,
@@ -44,8 +47,8 @@ class JobAdApi {
static createJobAd = async (data: JobAd): Promise<JobAd> => {
try {
return await postBackendAPI<JobAd, JobAd>({
path: APIPath.JOBADS, authenticated: true,
return await authedPostBackendAPI<JobAd, JobAd>({
path: APIPath.JOBADS,
}, data);
} catch (err) {
console.error(err);
@@ -55,8 +58,8 @@ class JobAdApi {
static updateJobAd = async (data: JobAd): Promise<JobAd> => {
try {
return await putBackendAPI<JobAd, JobAd>({
path: APIPath.JOBADS, urlParams: { id: data.id }, authenticated: true,
return await authedPutBackendAPI<JobAd, JobAd>({
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<void> => {
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;
+18 -15
View File
@@ -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<Signup> => {
try {
return await getBackendAPI<Signup>({
path: APIPath.SIGNUPS, urlParams: { id }, authenticated: true,
return await authedGetBackendAPI<Signup>({
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<Signup, Signup>({
return await authedPutBackendAPI<Signup, Signup>({
path: APIPath.SIGNUPS_EDIT,
urlParams: {
id,
@@ -54,7 +57,7 @@ class SignupApi {
static getSignupUUID = async (id: number, uuid: string): Promise<Signup> => {
try {
return await getBackendAPI<Signup>({
return await authedGetBackendAPI<Signup>({
path: APIPath.SIGNUPS_EDIT,
urlParams: {
id,
@@ -71,7 +74,7 @@ class SignupApi {
static deleteSignup = async (id: number): Promise<void> => {
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<SignupForm> => {
try {
return await getBackendAPI<SignupForm>({
return await authedGetBackendAPI<SignupForm>({
path: APIPath.SIGNUP_FORMS, urlParams: { id }, authenticated: auth,
});
} catch (err) {
@@ -91,7 +94,7 @@ class SignupApi {
static getForms = async (auth = false): Promise<SignupForm[]> => {
try {
return await getBackendAPI<SignupForm[]>({
return await authedGetBackendAPI<SignupForm[]>({
path: APIPath.SIGNUP_FORMS, authenticated: auth,
});
} catch (err) {
@@ -102,8 +105,8 @@ class SignupApi {
static createForm = async (data: SignupForm): Promise<SignupForm> => {
try {
return await postBackendAPI<SignupForm, SignupForm>({
path: APIPath.SIGNUP_FORMS, authenticated: true,
return await authedPostBackendAPI<SignupForm, SignupForm>({
path: APIPath.SIGNUP_FORMS,
}, data);
} catch (err) {
console.error(err);
@@ -113,8 +116,8 @@ class SignupApi {
static updateForm = async (data: SignupForm): Promise<SignupForm> => {
try {
return await putBackendAPI<SignupForm, SignupForm>({
path: APIPath.SIGNUP_FORMS, urlParams: { id: data.id }, authenticated: true,
return await authedPutBackendAPI<SignupForm, SignupForm>({
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<void> => {
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<void> => {
try {
await postBackendAPI<EmailRequest, { message: "Email sent" }>({ path: APIPath.SIGNUP_FORMS_EMAIL, urlParams: { id }, authenticated: true }, data);
await authedPostBackendAPI<EmailRequest, { message: "Email sent" }>({ 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<Signup[]> => {
try {
return await getBackendAPI<Signup[]>({ path: APIPath.SIGNUP_FORMS_SIGNUPS, urlParams: { id }, authenticated: true });
return await authedGetBackendAPI<Signup[]>({ path: APIPath.SIGNUP_FORMS_SIGNUPS, urlParams: { id } });
} catch (err) {
console.error(err);
throw err;