From a8b150857c17cc6cffffc0283572ea9ddba62411 Mon Sep 17 00:00:00 2001 From: John Costa Date: Sun, 5 Oct 2025 19:56:48 +0100 Subject: [PATCH] fix --- frontend/src/network/index.ts | 33 +++++++++++++++++++++------- frontend/src/pages/front/gallery.tsx | 2 +- frontend/src/pages/front/recent.tsx | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/frontend/src/network/index.ts b/frontend/src/network/index.ts index 29f327d..f0dadae 100644 --- a/frontend/src/network/index.ts +++ b/frontend/src/network/index.ts @@ -23,8 +23,8 @@ type BaseRequestParams = Partial<{ method: "GET" | "POST" | "DELETE"; }>; -// export const base = "https://haystack.johncosta.tech"; -export const base = "http://john-desktop:3040"; +export const base = "https://haystack.johncosta.tech"; +// export const base = "http://192.168.1.199:3040"; const getBaseRequest = ({ path, body, method }: BaseRequestParams): Request => { return new Request(`${base}/${path}`, { @@ -42,25 +42,42 @@ export const getAccessToken = async (): Promise => { const refreshToken = localStorage.getItem("refresh")?.toString(); if (accessToken == null && refreshToken == null) { - throw new Error("your are not logged in") + throw new Error("you are not logged in") } - const isValidAccessToken = accessToken != null && getTokenProperties(accessToken).exp.getTime() * 1000 > Date.now() + // FIX: Check what getTokenProperties returns + const tokenProps = getTokenProperties(accessToken!); + + // If tokenProps.exp is a number (seconds), convert to milliseconds: + const expiryTime = typeof tokenProps.exp === 'number' + ? tokenProps.exp * 1000 // Convert seconds to milliseconds + : tokenProps.exp.getTime(); // Already a Date object + + const isValidAccessToken = accessToken != null && expiryTime > Date.now(); + + console.log('Token check:', { + expiryTime: new Date(expiryTime), + now: new Date(), + isValid: isValidAccessToken, + timeLeft: (expiryTime - Date.now()) / 1000 + 's' + }); if (!isValidAccessToken) { + console.log('Refreshing token...'); const newAccessToken = await fetch(getBaseRequest({ - path: 'auth/refresh', method: "POST", body: JSON.stringify({ + path: 'auth/refresh', + method: "POST", + body: JSON.stringify({ refresh: refreshToken, }) })).then(r => r.json()); const { access } = parse(refreshTokenValidator, newAccessToken); - localStorage.setItem("access", access); - accessToken = access + accessToken = access; } - return accessToken! + return accessToken!; } const getBaseAuthorizedRequest = async ({ diff --git a/frontend/src/pages/front/gallery.tsx b/frontend/src/pages/front/gallery.tsx index 5c939a5..f30581e 100644 --- a/frontend/src/pages/front/gallery.tsx +++ b/frontend/src/pages/front/gallery.tsx @@ -39,7 +39,7 @@ export const Categories: Component = () => { return (

Generated stacks

-
+
{(stack) => }
diff --git a/frontend/src/pages/front/recent.tsx b/frontend/src/pages/front/recent.tsx index b5fa5b5..22bd54e 100644 --- a/frontend/src/pages/front/recent.tsx +++ b/frontend/src/pages/front/recent.tsx @@ -18,7 +18,7 @@ export const Recent: Component = () => { return (

Recent Screenshots

-
+
{(image) => }