From 9647e890f6f61ab715ff6b11018279ad877c84b9 Mon Sep 17 00:00:00 2001 From: julius Date: Mon, 17 Feb 2025 22:28:48 +0100 Subject: [PATCH] feat: simple OAuth2 login with JWT token --- src/Analysis.tsx | 7 ++----- src/App.tsx | 6 +++--- src/Rankings.tsx | 3 ++- src/api.ts | 15 ++------------- 4 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/Analysis.tsx b/src/Analysis.tsx index dcb8728..7eec5c3 100644 --- a/src/Analysis.tsx +++ b/src/Analysis.tsx @@ -59,6 +59,7 @@ export default function Analysis() { const [showControlPanel, setShowControlPanel] = useState(false); const [loading, setLoading] = useState(true); + const auth = useAuthContext(); // Function to generate and fetch the graph image async function loadImage() { setLoading(true); @@ -67,8 +68,7 @@ export default function Analysis() { setImage(data.image); setLoading(false); }).catch((e) => { - const { checkAuth } = useAuthContext(); - checkAuth(); + auth.doLogin(); }) } @@ -89,9 +89,6 @@ export default function Analysis() { } } - const { user } = useAuthContext()! - console.log(`logged in as ${user.username}`); - return (
- assign as many or as few players as you want and don't forget to submit (💾) when you're done :) + assign as many or as few players as you want
+ and don't forget to submit (💾) when you're done :)
diff --git a/src/api.ts b/src/api.ts index e90669a..3a48903 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,7 +1,3 @@ -import { useContext } from "react"; -import useAuthContext from "./AuthContext"; -import { createCookie } from "react-router"; - export const baseUrl = import.meta.env.VITE_BASE_URL as string; export const token = () => localStorage.getItem("access_token") as string; @@ -90,15 +86,8 @@ export const login = (req: LoginRequest) => { method: "POST", headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: new URLSearchParams(req).toString() - }).then(resp => resp.json() as unknown as 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")); -} - -export const cookielogin = (req: LoginRequest) => { - fetch(`${baseUrl}api/token`, { - method: "POST", headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, body: new URLSearchParams(req).toString() - }).then(resp => { createCookie(resp.headers.getSetCookie()) }).catch((e) => console.log("catch error " + e + " in login")); + }).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");