From 2a01ca43b2385ad3058458ebc6b59742d2136760 Mon Sep 17 00:00:00 2001 From: Julian Lobbes Date: Sat, 17 Jun 2023 19:30:47 +0200 Subject: [PATCH] squashme --- backend/todo/routes/auth.py | 2 +- backend/todo/routes/users.py | 7 ++++++- frontend/src/lib/api/endpoints.ts | 4 ++-- frontend/src/lib/api/types.ts | 1 - frontend/src/lib/auth/session.ts | 25 +++++++++++++------------ frontend/src/routes/login/+page.svelte | 9 +++++++++ 6 files changed, 31 insertions(+), 17 deletions(-) diff --git a/backend/todo/routes/auth.py b/backend/todo/routes/auth.py index c640793..6f77697 100644 --- a/backend/todo/routes/auth.py +++ b/backend/todo/routes/auth.py @@ -22,7 +22,7 @@ tag_metadata = { } -@router.post("/login", response_model=AuthResponseToken) +@router.post("/login/", response_model=AuthResponseToken) def login(credentials: UserLoginSchema, db: Session = Depends(get_db)): """Returns a JWT for the user whose credentials were provided. diff --git a/backend/todo/routes/users.py b/backend/todo/routes/users.py index 0a07c90..e7fdecb 100644 --- a/backend/todo/routes/users.py +++ b/backend/todo/routes/users.py @@ -2,7 +2,7 @@ from typing import Annotated -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, Depends, HTTPException, Request from sqlalchemy.orm import Session from todo.database.engine import get_db @@ -13,6 +13,9 @@ from todo.utils.exceptions import create_exception_dict as fmt from todo.dependencies.users import UserSortablePaginationParams import todo.auth.auth as auth +import logging +logger = logging.getLogger() + router = APIRouter( prefix="/users", @@ -48,9 +51,11 @@ def create_user( @router.get("/{user_id}", response_model=userschema.User) def read_user( user_id: int, + request: Request, db: Session = Depends(get_db), current_user: userschema.User = Depends(auth_handler.get_current_user), ): + logger.error(request) try: user = usercrud.read_user(db=db, id=user_id) except NotFoundException as e: diff --git a/frontend/src/lib/api/endpoints.ts b/frontend/src/lib/api/endpoints.ts index 5d76004..e1eb696 100644 --- a/frontend/src/lib/api/endpoints.ts +++ b/frontend/src/lib/api/endpoints.ts @@ -2,7 +2,7 @@ import { error } from '@sveltejs/kit'; import type { ItemCount, TodoItem, Token, User } from './types'; import { Endpoint } from './types'; import type { StringMapping } from '$lib/utils/types'; -import { getTokenFromLocalstorage } from '$lib/auth/session'; +import { _getTokenFromLocalstorage } from '$lib/auth/session'; /** * A factory class for creating `Endpoint` instances to interact with the backend API. @@ -41,7 +41,7 @@ export class EndpointFactory { */ constructor(fetchFunction: Function = fetch) { this.fetchFunction = fetchFunction; - this._jwt = getTokenFromLocalstorage(); + this._jwt = _getTokenFromLocalstorage(); } private _getDefaultHeaders(): StringMapping { diff --git a/frontend/src/lib/api/types.ts b/frontend/src/lib/api/types.ts index 2c86811..5373ed0 100644 --- a/frontend/src/lib/api/types.ts +++ b/frontend/src/lib/api/types.ts @@ -101,7 +101,6 @@ export class Endpoint { if ('queryParameters' in options) { endpointUrl += `?${new URLSearchParams(options.queryParameters)}`; } - console.log(endpointUrl) const response = await this.fetchFunction(endpointUrl, { method: this.requestMethod, diff --git a/frontend/src/lib/auth/session.ts b/frontend/src/lib/auth/session.ts index 7df1519..40c7c58 100644 --- a/frontend/src/lib/auth/session.ts +++ b/frontend/src/lib/auth/session.ts @@ -9,10 +9,10 @@ import { writable } from 'svelte/store'; export const storedUser = writable(); // Name of the key holding the auth JWT in localstorage -const jwtKey = 'jwt'; +const _jwtKey = 'jwt'; // Name of the key holding the authenticated user in localstorage -const userKey = 'user'; +const _userKey = 'user'; export type StoredUser = { id: number, email: string, @@ -25,22 +25,22 @@ export type StoredUser = { * * @param {string} token - The token to save in localstorage. */ -function saveTokenToLocalstorage(token: string): void { - localStorage.setItem(jwtKey, token); +function _saveTokenToLocalstorage(token: string): void { + localStorage.setItem(_jwtKey, token); } /** * Retrieves and returns the token, if present, from localstorage. */ -export function getTokenFromLocalstorage(): string | null { - return localStorage.getItem(jwtKey); +export function _getTokenFromLocalstorage(): string | null { + return localStorage.getItem(_jwtKey); } /** * Removes the saved token from localstorage. */ function clearTokenInLocalstorage(): void { - localStorage.removeItem(jwtKey); + localStorage.removeItem(_jwtKey); } /** @@ -49,14 +49,14 @@ function clearTokenInLocalstorage(): void { * @param {StoredUser} user - The user to write to localstorage. */ function saveUserToLocalstorage(user: StoredUser): void { - localStorage.setItem(userKey, JSON.stringify(user)); + localStorage.setItem(_userKey, JSON.stringify(user)); } /** * Retrieves and returns the user, if present, from localstorage. */ export function getUserFromLocalstorage(): StoredUser | null { - let item: string | null = localStorage.getItem(userKey); + let item: string | null = localStorage.getItem(_userKey); if (typeof item !== 'string') { return null; } @@ -67,7 +67,7 @@ export function getUserFromLocalstorage(): StoredUser | null { * Removes the saved user from localstorage. */ function clearUserInLocalstorage(): void { - localStorage.removeItem(userKey); + localStorage.removeItem(_userKey); } /** @@ -112,12 +112,13 @@ export async function login(email: string, password: string): Promise { const parsedToken = jwt_decode(token.token) as Token; const userId = parsedToken.sub; - saveTokenToLocalstorage(token.token); + _saveTokenToLocalstorage(token.token); // recreate the factory with the jwt now in localstorage endpointFactory = new EndpointFactory(); const readUserEndpoint = endpointFactory.createReadUserEndpoint(userId); - console.log(readUserEndpoint.call()) + console.log(readUserEndpoint) + console.log(await readUserEndpoint.call()) const user = await readUserEndpoint.call(); saveUserToLocalstorage({ diff --git a/frontend/src/routes/login/+page.svelte b/frontend/src/routes/login/+page.svelte index b3a019b..45d6447 100644 --- a/frontend/src/routes/login/+page.svelte +++ b/frontend/src/routes/login/+page.svelte @@ -2,6 +2,8 @@ import { goto } from '$app/navigation'; import { login } from '$lib/auth/session'; + import { EndpointFactory } from '$lib/api/endpoints'; + let email: string = ""; let password: string = ""; @@ -37,6 +39,12 @@ formError = true; } } + + async function handleDebugButtonClick() { + let epf = new EndpointFactory(); + let rue = epf.createReadUserEndpoint(54); + console.log(await rue.call()); + }
@@ -58,6 +66,7 @@ {/if} +