squashme
This commit is contained in:
parent
5c0f557308
commit
2a01ca43b2
6 changed files with 31 additions and 17 deletions
|
@ -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.
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -101,7 +101,6 @@ export class Endpoint<T> {
|
|||
if ('queryParameters' in options) {
|
||||
endpointUrl += `?${new URLSearchParams(options.queryParameters)}`;
|
||||
}
|
||||
console.log(endpointUrl)
|
||||
|
||||
const response = await this.fetchFunction(endpointUrl, {
|
||||
method: this.requestMethod,
|
||||
|
|
|
@ -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<void> {
|
|||
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({
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col items-center gap-y-4">
|
||||
|
@ -58,6 +66,7 @@
|
|||
{/if}
|
||||
</fieldset>
|
||||
</form>
|
||||
<button on:click={handleDebugButtonClick}>Click me</button>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
|
|
Loading…
Add table
Reference in a new issue