feat: add correct keys to User
interface
This commit is contained in:
parent
b8c4190072
commit
b7c8136b1e
109
src/api.ts
109
src/api.ts
@ -18,46 +18,18 @@ export default async function api(path: string, data: any): Promise<any> {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function apiAuth(path: string, data: any, method: string = "GET"): Promise<any> {
|
export async function apiAuth(
|
||||||
|
path: string,
|
||||||
const req = new Request(`${baseUrl}api/${path}`,
|
data: any,
|
||||||
{
|
method: string = "GET"
|
||||||
method: method,
|
): Promise<any> {
|
||||||
headers: {
|
const req = new Request(`${baseUrl}api/${path}`, {
|
||||||
"Authorization": `Bearer ${token()} `,
|
method: method,
|
||||||
'Content-Type': 'application/json'
|
headers: {
|
||||||
},
|
Authorization: `Bearer ${token()} `,
|
||||||
...(data && { body: JSON.stringify(data) })
|
"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'
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
let resp: Response;
|
let resp: Response;
|
||||||
try {
|
try {
|
||||||
@ -68,8 +40,40 @@ export async function currentUser(): Promise<User> {
|
|||||||
|
|
||||||
if (!resp.ok) {
|
if (!resp.ok) {
|
||||||
if (resp.status === 401) {
|
if (resp.status === 401) {
|
||||||
logout()
|
logout();
|
||||||
throw new Error('Unauthorized');
|
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>;
|
return resp.json() as Promise<User>;
|
||||||
@ -86,11 +90,20 @@ export type Token = {
|
|||||||
|
|
||||||
export const login = (req: LoginRequest) => {
|
export const login = (req: LoginRequest) => {
|
||||||
fetch(`${baseUrl}api/token`, {
|
fetch(`${baseUrl}api/token`, {
|
||||||
method: "POST", headers: {
|
method: "POST",
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
headers: {
|
||||||
}, body: new URLSearchParams(req).toString()
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
}).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>
|
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");
|
export const logout = () => localStorage.removeItem("access_token");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user