feat: roll back refresh tokens, use single token only
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Dispatch, SetStateAction, useEffect, useMemo, useState } from "react";
|
||||
import { ReactSortable, ReactSortableProps } from "react-sortablejs";
|
||||
import api, { baseUrl } from "./api";
|
||||
import { apiAuth } from "./api";
|
||||
|
||||
interface Player {
|
||||
id: number;
|
||||
@@ -124,7 +124,7 @@ export function Chemistry({ user, players }: PlayerInfoProps) {
|
||||
let middle = playersMiddle.map(({ name }) => name);
|
||||
let right = playersRight.map(({ name }) => name);
|
||||
const data = { user: _user, hate: left, undecided: middle, love: right };
|
||||
const response = await api("chemistry", data);
|
||||
const response = await apiAuth("chemistry", data);
|
||||
response.ok ? setDialog("success!") : setDialog("try sending again");
|
||||
}
|
||||
}
|
||||
@@ -203,7 +203,7 @@ export function MVP({ user, players }: PlayerInfoProps) {
|
||||
let _user = user.map(({ name }) => name)[0];
|
||||
let mvps = rankedPlayers.map(({ name }) => name);
|
||||
const data = { user: _user, mvps: mvps };
|
||||
const response = await api("mvps", data);
|
||||
const response = await apiAuth("mvps", data);
|
||||
response.ok ? setDialog("success!") : setDialog("try sending again");
|
||||
}
|
||||
}
|
||||
@@ -272,10 +272,7 @@ export default function Rankings() {
|
||||
const [openTab, setOpenTab] = useState("Chemistry");
|
||||
|
||||
async function loadPlayers() {
|
||||
const response = await fetch(`${baseUrl}api/player/list`, {
|
||||
method: "GET",
|
||||
});
|
||||
const data = await response.json();
|
||||
const data = await apiAuth("player/list", null, "GET");
|
||||
setPlayers(data as Player[]);
|
||||
}
|
||||
|
||||
@@ -334,8 +331,11 @@ export default function Rankings() {
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<span className="grey">assign as many or as few players as you want<br />
|
||||
and don't forget to <b>submit</b> (💾) when you're done :)</span>
|
||||
<span className="grey">
|
||||
assign as many or as few players as you want
|
||||
<br />
|
||||
and don't forget to <b>submit</b> (💾) when you're done :)
|
||||
</span>
|
||||
|
||||
<div id="Chemistry" className="tabcontent">
|
||||
<Chemistry {...{ user, players }} />
|
||||
|
46
src/api.ts
46
src/api.ts
@@ -1,22 +1,4 @@
|
||||
export const baseUrl = import.meta.env.VITE_BASE_URL as string;
|
||||
export const token = () => localStorage.getItem("access_token") as string;
|
||||
|
||||
export default async function api(path: string, data: any): Promise<any> {
|
||||
const request = new Request(`${baseUrl}${path}/`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(request);
|
||||
} catch (e) {
|
||||
throw new Error(`request failed: ${e}`);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function apiAuth(
|
||||
path: string,
|
||||
@@ -26,9 +8,9 @@ export async function apiAuth(
|
||||
const req = new Request(`${baseUrl}api/${path}`, {
|
||||
method: method,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token()} `,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
credentials: "include",
|
||||
...(data && { body: JSON.stringify(data) }),
|
||||
});
|
||||
let resp: Response;
|
||||
@@ -55,13 +37,12 @@ export type User = {
|
||||
};
|
||||
|
||||
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",
|
||||
},
|
||||
credentials: "include",
|
||||
});
|
||||
let resp: Response;
|
||||
try {
|
||||
@@ -83,12 +64,7 @@ export type LoginRequest = {
|
||||
username: string;
|
||||
password: string;
|
||||
};
|
||||
export type Token = {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
};
|
||||
|
||||
// api.js
|
||||
export const login = async (req: LoginRequest): Promise<void> => {
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}api/token`, {
|
||||
@@ -97,20 +73,24 @@ export const login = async (req: LoginRequest): Promise<void> => {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
body: new URLSearchParams(req).toString(),
|
||||
credentials: "include",
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
const token = (await response.json()) as Token;
|
||||
if (token && token.access_token) {
|
||||
localStorage.setItem("access_token", token.access_token);
|
||||
} else {
|
||||
console.log("Token not acquired");
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
throw e; // rethrow the error so it can be caught by the caller
|
||||
}
|
||||
};
|
||||
|
||||
export const logout = () => localStorage.removeItem("access_token");
|
||||
export const logout = async () => {
|
||||
try {
|
||||
await fetch(`${baseUrl}api/logout`, {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user