feat: add correct keys to User interface

This commit is contained in:
julius 2025-03-05 09:57:03 +01:00
parent b8c4190072
commit b7c8136b1e
Signed by: julius
GPG Key ID: C80A63E6A5FD7092

View File

@ -18,46 +18,18 @@ export default async function api(path: string, data: any): Promise<any> {
return response;
}
export async function apiAuth(path: string, data: any, method: string = "GET"): Promise<any> {
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<User> {
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<any> {
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<User> {
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<User> {
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<User>;
@ -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<Token>).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<void>
}
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams(req).toString(),
})
.then((resp) => resp.json() as Promise<Token>)
.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<void>;
};
export const logout = () => localStorage.removeItem("access_token");