feat: simple OAuth2 login with JWT token

This commit is contained in:
julius 2025-02-17 22:28:48 +01:00
parent 15c9a64de2
commit 9647e890f6
Signed by: julius
GPG Key ID: C80A63E6A5FD7092
4 changed files with 9 additions and 22 deletions

View File

@ -59,6 +59,7 @@ export default function Analysis() {
const [showControlPanel, setShowControlPanel] = useState(false); const [showControlPanel, setShowControlPanel] = useState(false);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const auth = useAuthContext();
// Function to generate and fetch the graph image // Function to generate and fetch the graph image
async function loadImage() { async function loadImage() {
setLoading(true); setLoading(true);
@ -67,8 +68,7 @@ export default function Analysis() {
setImage(data.image); setImage(data.image);
setLoading(false); setLoading(false);
}).catch((e) => { }).catch((e) => {
const { checkAuth } = useAuthContext(); auth.doLogin();
checkAuth();
}) })
} }
@ -89,9 +89,6 @@ export default function Analysis() {
} }
} }
const { user } = useAuthContext()!
console.log(`logged in as ${user.username}`);
return ( return (
<div className="stack column dropdown"> <div className="stack column dropdown">
<button onClick={() => setShowControlPanel(!showControlPanel)}> <button onClick={() => setShowControlPanel(!showControlPanel)}>

View File

@ -1,10 +1,10 @@
import Analysis from "./Analysis"; import Analysis from "./Analysis";
import "./App.css"; import "./App.css";
import { AuthProvider } from "./AuthContext";
import Footer from "./Footer"; import Footer from "./Footer";
import Header from "./Header"; import Header from "./Header";
import Rankings from "./Rankings"; import Rankings from "./Rankings";
import { BrowserRouter, Routes, Route } from "react-router"; import { BrowserRouter, Routes, Route } from "react-router";
import { SessionProvider } from "./Session";
function App() { function App() {
//const [data, setData] = useState({ nodes: [], links: [] } as SociogramData); //const [data, setData] = useState({ nodes: [], links: [] } as SociogramData);
@ -19,9 +19,9 @@ function App() {
<Routes> <Routes>
<Route index element={<Rankings />} /> <Route index element={<Rankings />} />
<Route path="/analysis" element={ <Route path="/analysis" element={
<AuthProvider> <SessionProvider>
<Analysis /> <Analysis />
</AuthProvider> </SessionProvider>
} /> } />
</Routes> </Routes>
<Footer /> <Footer />

View File

@ -334,7 +334,8 @@ export default function Rankings() {
</button> </button>
</div> </div>
<span className="grey">assign as many or as few players as you want 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"> <div id="Chemistry" className="tabcontent">
<Chemistry {...{ user, players }} /> <Chemistry {...{ user, players }} />

View File

@ -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 baseUrl = import.meta.env.VITE_BASE_URL as string;
export const token = () => localStorage.getItem("access_token") as string; export const token = () => localStorage.getItem("access_token") as string;
@ -90,15 +86,8 @@ export const login = (req: LoginRequest) => {
method: "POST", headers: { method: "POST", headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
}, body: new URLSearchParams(req).toString() }, 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")); }).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 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"));
} }
export const logout = () => localStorage.removeItem("access_token"); export const logout = () => localStorage.removeItem("access_token");