diff --git a/src/api.ts b/src/api.ts index 48cae36..96beebb 100644 --- a/src/api.ts +++ b/src/api.ts @@ -18,46 +18,18 @@ export default async function api(path: string, data: any): Promise { return response; } -export async function apiAuth(path: string, data: any, method: string = "GET"): Promise { - - const req = new Request(`${baseUrl}api/${path}`, - { - method: method, - headers: { - "Authorization": `Bearer ${token()} `, - 'Content-Type': 'application/json' - }, - ...(data && { body: JSON.stringify(data) }) - } - ); - let resp: Response; - try { - resp = await fetch(req); - } catch (e) { - throw new Error(`request failed: ${e}`); - } - - if (!resp.ok) { - if (resp.status === 401) { - logout() - throw new Error('Unauthorized'); - } - } - return resp.json() -} - -export type User = { - username: string; - fullName: string; -} - -export async function currentUser(): Promise { - if (!token()) throw new Error("you have no access token") - const req = new Request(`${baseUrl}api/users/me/`, { - method: "GET", headers: { - "Authorization": `Bearer ${token()} `, - 'Content-Type': 'application/json' - } +export async function apiAuth( + path: string, + data: any, + method: string = "GET" +): Promise { + const req = new Request(`${baseUrl}api/${path}`, { + method: method, + headers: { + Authorization: `Bearer ${token()} `, + "Content-Type": "application/json", + }, + ...(data && { body: JSON.stringify(data) }), }); let resp: Response; try { @@ -68,8 +40,40 @@ export async function currentUser(): Promise { if (!resp.ok) { if (resp.status === 401) { - logout() - throw new Error('Unauthorized'); + logout(); + throw new Error("Unauthorized"); + } + } + return resp.json(); +} + +export type User = { + username: string; + full_name: string; + email: string; + player_id: number; +}; + +export async function currentUser(): Promise { + if (!token()) throw new Error("you have no access token"); + const req = new Request(`${baseUrl}api/users/me/`, { + method: "GET", + headers: { + Authorization: `Bearer ${token()} `, + "Content-Type": "application/json", + }, + }); + let resp: Response; + try { + resp = await fetch(req); + } catch (e) { + throw new Error(`request failed: ${e}`); + } + + if (!resp.ok) { + if (resp.status === 401) { + logout(); + throw new Error("Unauthorized"); } } return resp.json() as Promise; @@ -86,11 +90,20 @@ export type Token = { export const login = (req: LoginRequest) => { fetch(`${baseUrl}api/token`, { - method: "POST", headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, body: new URLSearchParams(req).toString() - }).then(resp => resp.json() as Promise).then(token => token ? localStorage.setItem("access_token", token.access_token) : console.log("token not acquired")).catch((e) => console.log("catch error " + e + " in login")); - return Promise -} + method: "POST", + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + body: new URLSearchParams(req).toString(), + }) + .then((resp) => resp.json() as Promise) + .then((token) => + token + ? localStorage.setItem("access_token", token.access_token) + : console.log("token not acquired") + ) + .catch((e) => console.log("catch error " + e + " in login")); + return Promise; +}; export const logout = () => localStorage.removeItem("access_token");