From 15c9a64de26fe903d3eca703efbb12c1ec18a59e Mon Sep 17 00:00:00 2001 From: julius Date: Sun, 16 Feb 2025 17:22:36 +0100 Subject: [PATCH] feat: inelegant and buggy version of auth --- security.py | 7 ++-- src/Analysis.tsx | 20 +++++------ src/App.css | 17 +++++++--- src/App.tsx | 9 +++-- src/api.ts | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 19 deletions(-) diff --git a/security.py b/security.py index dca1e37..71e1d84 100644 --- a/security.py +++ b/security.py @@ -1,6 +1,6 @@ from datetime import timedelta, timezone, datetime from typing import Annotated -from fastapi import Depends, HTTPException, status +from fastapi import Depends, HTTPException, Response, status from pydantic import BaseModel import jwt from jwt.exceptions import InvalidTokenError @@ -102,7 +102,7 @@ async def get_current_active_user( async def login_for_access_token( - form_data: Annotated[OAuth2PasswordRequestForm, Depends()], + form_data: Annotated[OAuth2PasswordRequestForm, Depends()], response: Response ) -> Token: user = authenticate_user(form_data.username, form_data.password) if not user: @@ -115,6 +115,9 @@ async def login_for_access_token( access_token = create_access_token( data={"sub": user.username}, expires_delta=access_token_expires ) + response.set_cookie( + "Authorization", value=f"Bearer {access_token}", httponly=True, samesite="none" + ) return Token(access_token=access_token, token_type="bearer") diff --git a/src/Analysis.tsx b/src/Analysis.tsx index a60a6b3..dcb8728 100644 --- a/src/Analysis.tsx +++ b/src/Analysis.tsx @@ -1,5 +1,6 @@ import { useEffect, useState } from "react"; -import { baseUrl } from "./api"; +import { apiAuth, baseUrl, token } from "./api"; +import useAuthContext from "./AuthContext"; //const debounce = void>( // func: T, @@ -61,18 +62,14 @@ export default function Analysis() { // Function to generate and fetch the graph image async function loadImage() { setLoading(true); - await fetch(`${baseUrl}api/analysis/image`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(params) - }) - .then((resp) => resp.json()) + await apiAuth("analysis/image", params, "POST") .then((data) => { setImage(data.image); setLoading(false); - }); + }).catch((e) => { + const { checkAuth } = useAuthContext(); + checkAuth(); + }) } useEffect(() => { @@ -92,6 +89,9 @@ export default function Analysis() { } } + const { user } = useAuthContext()! + console.log(`logged in as ${user.username}`); + return (