The weeb for the next gen discord boat - Wamellow
wamellow.com
bot
discord
1import type { User } from "@/common/user";
2import type { ApiError } from "@/typings";
3import type React from "react";
4
5enum State {
6 Idle = 0,
7 Loading = 1,
8 Failure = 2
9}
10
11export async function authorize({
12 setState
13}: {
14 setState: React.Dispatch<React.SetStateAction<State>>;
15}) {
16 setState(State.Idle);
17
18 const res = await fetch(`${process.env.NEXT_PUBLIC_API}/sessions`, {
19 credentials: "include"
20 })
21 .then((res) => res.json())
22 .catch(() => null) as User | ApiError | null;
23
24 if (res && "status" in res && res.status.toString().startsWith("4")) {
25 window.location.href = "/login";
26 return null;
27 }
28
29 if (!res) {
30 setState(State.Failure);
31 return null;
32 }
33
34 setState(State.Idle);
35 return res;
36}