Skip to content

Commit

Permalink
feat: rewrite auth() function
Browse files Browse the repository at this point in the history
  • Loading branch information
qamarq committed Dec 9, 2024
1 parent cc95e13 commit fac586d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
63 changes: 29 additions & 34 deletions frontend/src/actions/plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,22 @@ export const createNewPlan = async ({
registrations: Array<{ id: string }>;
groups: Array<{ id: number }>;
}) => {
const isLogged = await auth({});
if (!isLogged) {
throw new Error("Not logged in");
}
try {
await auth();

const data = await fetchToAdonis<CreatePlanResponseType>({
url: "/user/schedules",
method: "POST",
body: JSON.stringify({ name, courses, registrations, groups }),
});
if (!data) {
throw new Error("Failed to create new plan");
const data = await fetchToAdonis<CreatePlanResponseType>({
url: "/user/schedules",
method: "POST",
body: JSON.stringify({ name, courses, registrations, groups }),
});
if (!data) {
throw new Error("Failed to create new plan");
}
revalidatePath("/plans");
return data;
} catch (e) {
throw new Error("Not logged in");
}
return data;
};

export const updatePlan = async ({
Expand All @@ -89,10 +91,7 @@ export const updatePlan = async ({
registrations: Array<{ id: string }>;
groups: Array<{ id: number }>;
}) => {
const isLogged = await auth({});
if (!isLogged) {
throw new Error("Not logged in");
}
await auth();

const data = await fetchToAdonis<CreatePlanResponseType>({
url: `/user/schedules/${id}`,
Expand All @@ -106,29 +105,25 @@ export const updatePlan = async ({
};

export const deletePlan = async ({ id }: { id: number }) => {
const isLogged = await auth({});
if (!isLogged) {
try {
await auth();
const data = await fetchToAdonis<DeletePlanResponseType>({
url: `/user/schedules/${id}`,
method: "DELETE",
});
if (!data) {
throw new Error("Failed to delete plan");
}
revalidatePath("/plans");
return data;
} catch (e) {
throw new Error("Not logged in");
}

const data = await fetchToAdonis<DeletePlanResponseType>({
url: `/user/schedules/${id}`,
method: "DELETE",
});
if (!(data?.success ?? false)) {
throw new Error("Failed to delete plan");
}
revalidatePath("/plans");
return data;
};

export const getPlan = async ({ id }: { id: number }) => {
const isLogged = await auth({});
if (!isLogged) {
return false;
}

try {
await auth();
const data = await fetchToAdonis<PlanResponseType>({
url: `/user/schedules/${id}`,
method: "GET",
Expand All @@ -138,6 +133,6 @@ export const getPlan = async ({ id }: { id: number }) => {
}
return data;
} catch (e) {
return false;
return null;
}
};
37 changes: 21 additions & 16 deletions frontend/src/lib/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,17 @@ export async function getRequestToken() {
};
}

export const auth = async ({
token,
secret,
}: {
token?: string | null;
secret?: string | null;
export const auth = async (tokens?: {
token: string | undefined;
secret: string | undefined;
}) => {
const cookies = await cookiesPromise();
const accessToken = token ?? cookies.get("access_token")?.value;
const accessSecret = secret ?? cookies.get("access_token_secret")?.value;
const accessToken = tokens?.token ?? cookies.get("access_token")?.value;
const accessSecret =
tokens?.secret ?? cookies.get("access_token_secret")?.value;

if (accessToken === "" || accessSecret === "") {
return false;
throw new Error("No access token or access secret");
}

try {
Expand All @@ -117,8 +115,15 @@ export const auth = async ({
body: JSON.stringify({ accessToken, accessSecret }),
credentials: "include",
});
const data = (await response.json()) as { error?: string };
if (data.error !== undefined) {
const data = (await response.json()) as
| {
firstName: string;
lastName: string;
studentNumber: number;
usosId: string;
}
| { error: string };
if ("error" in data) {
cookies.delete({
name: "access_token",
path: "/",
Expand All @@ -130,9 +135,9 @@ export const auth = async ({
throw new Error(data.error);
}
const setCookieHeaders = response.headers.getSetCookie();
setCookieHeaders.forEach((setCookie) => {
const name = setCookie.split("=")[0];
const value = setCookie.split("=")[1].split(";")[0];
setCookieHeaders.forEach((cookie) => {
const preparedCookie = cookie.replace(";", "");
const [name, value] = preparedCookie.split("=");
cookies.set({
name,
value,
Expand All @@ -142,9 +147,9 @@ export const auth = async ({
secure: true,
});
});
return true;
return data;
} catch (error) {
return false;
throw new Error("Failed to authenticate");
}
};

Expand Down

0 comments on commit fac586d

Please sign in to comment.