From 661f0254fd01ba6c521407cca0aef0b35a15328d Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 19 Apr 2023 00:41:20 +0200 Subject: [PATCH] fix: log user out if token is invalid (fixes #50) --- frontend/src/lib/api/fetch.ts | 43 +++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/frontend/src/lib/api/fetch.ts b/frontend/src/lib/api/fetch.ts index 20fa072..8448c75 100644 --- a/frontend/src/lib/api/fetch.ts +++ b/frontend/src/lib/api/fetch.ts @@ -1,6 +1,8 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import type { APIError } from "./entities"; +import { ErrorCode, type APIError } from "./entities"; import { PUBLIC_BASE_URL } from "$env/static/public"; +import { addToast } from "$lib/toast"; +import { userStore } from "$lib/store"; export async function apiFetch( path: string, @@ -26,8 +28,24 @@ export async function apiFetch( return data as T; } -export const apiFetchClient = async (path: string, method = "GET", body: any = null) => - apiFetch(path, { method, body, token: localStorage.getItem("pronouns-token") || undefined }); +export const apiFetchClient = async (path: string, method = "GET", body: any = null) => { + try { + const data = await apiFetch(path, { + method, + body, + token: localStorage.getItem("pronouns-token") || undefined, + }); + return data; + } catch (e) { + if ((e as APIError).code === ErrorCode.InvalidToken) { + addToast({ header: "Token expired", body: "Your token has expired, please log in again." }); + userStore.set(null); + localStorage.removeItem("pronouns-token"); + localStorage.removeItem("pronouns-user"); + } + throw e; + } +}; /** Fetches the specified path without parsing the response body. */ export async function fastFetch( @@ -53,5 +71,20 @@ export async function fastFetch( } /** Fetches the specified path without parsing the response body. */ -export const fastFetchClient = async (path: string, method = "GET", body: any = null) => - fastFetch(path, { method, body, token: localStorage.getItem("pronouns-token") || undefined }); +export const fastFetchClient = async (path: string, method = "GET", body: any = null) => { + try { + await fastFetch(path, { + method, + body, + token: localStorage.getItem("pronouns-token") || undefined, + }); + } catch (e) { + if ((e as APIError).code === ErrorCode.InvalidToken) { + addToast({ header: "Token expired", body: "Your token has expired, please log in again." }); + userStore.set(null); + localStorage.removeItem("pronouns-token"); + localStorage.removeItem("pronouns-user"); + } + throw e; + } +};