From cc3d138fd33a6835be020bc336186d89556c80c1 Mon Sep 17 00:00:00 2001 From: john Date: Tue, 20 May 2025 23:43:43 +0200 Subject: [PATCH] fix user refresh --- src/app/api/client.ts | 3 --- src/app/auth/authService.ts | 5 ----- src/app/auth/components/RefreshUser.tsx | 2 +- src/app/feed/pages/HomePage.tsx | 2 +- src/app/user/user.ts | 4 ++-- src/useRefreshSessionLoop.ts | 3 ++- 6 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/app/api/client.ts b/src/app/api/client.ts index c840b08..19b8899 100644 --- a/src/app/api/client.ts +++ b/src/app/api/client.ts @@ -8,9 +8,6 @@ export function initClient(): ApiClient { const client = createClient({ baseUrl: import.meta.env.VITE_API_URL }) const UnauthorizedHandlerMiddleware: Middleware = { async onResponse({ response }) { - console.debug('on response middleware?') - console.debug(response.headers.getSetCookie()) - console.debug(response.headers.get('set-cookie')) if (response.status === 401) { dispatchMessage('auth:unauthorized', null) } diff --git a/src/app/auth/authService.ts b/src/app/auth/authService.ts index 7916caf..51e56fa 100644 --- a/src/app/auth/authService.ts +++ b/src/app/auth/authService.ts @@ -1,7 +1,6 @@ import { dispatchMessage } from '../messageBus/messageBus.ts' import { ProblemDetails } from '../../types' import { SignupCode } from './signupCode.ts' -import { getCookie } from './cookies.ts' import { ApiClient } from '../api/client.ts' export class AuthService { @@ -66,10 +65,6 @@ export class AuthService { } async refreshUser(userId: string) { - if (getCookie('hasSession') !== 'true') { - return null - } - await this.client.GET(`/auth/user/{userId}`, { params: { path: { userId }, diff --git a/src/app/auth/components/RefreshUser.tsx b/src/app/auth/components/RefreshUser.tsx index b49bb1e..8796381 100644 --- a/src/app/auth/components/RefreshUser.tsx +++ b/src/app/auth/components/RefreshUser.tsx @@ -18,7 +18,7 @@ export default function RefreshUser({ if (didRefresh.current) return if (user == null) return didRefresh.current = true - await authService.refreshUser(user.userId) + await authService.refreshUser(user.id) }) return () => clearTimeout(timeoutId) }, [authService, user]) diff --git a/src/app/feed/pages/HomePage.tsx b/src/app/feed/pages/HomePage.tsx index 7abd10e..976bf83 100644 --- a/src/app/feed/pages/HomePage.tsx +++ b/src/app/feed/pages/HomePage.tsx @@ -52,7 +52,7 @@ export default function HomePage({ postsService, mediaService }: HomePageProps) } }), ) - const postId = await postsService.createNew(user.userId, content, media, isPublic) + const postId = await postsService.createNew(user.id, content, media, isPublic) const post = new Post(postId, content, media, Temporal.Now.instant(), user.username) setPages((pages) => [[post], ...pages]) } catch (error) { diff --git a/src/app/user/user.ts b/src/app/user/user.ts index bd2505b..e5d041c 100644 --- a/src/app/user/user.ts +++ b/src/app/user/user.ts @@ -5,9 +5,9 @@ import { useState } from 'react' import { setGlobal } from '../femtoApp.ts' export interface User { - userId: string + id: string username: string - isSuperUser: boolean + roles: string[] } let globalUser: User | null diff --git a/src/useRefreshSessionLoop.ts b/src/useRefreshSessionLoop.ts index f34b6d5..36144ce 100644 --- a/src/useRefreshSessionLoop.ts +++ b/src/useRefreshSessionLoop.ts @@ -5,7 +5,7 @@ import { AuthService } from './app/auth/authService.ts' // Starts a loop that pings the server to keep the session alive, while also getting any updates on the user profile export function useRefreshSessionLoop(authService: AuthService) { const user = useUser() - const userId = user?.userId ?? null + const userId = user?.id ?? null useEffect(() => { if (userId == null) { @@ -17,6 +17,7 @@ export function useRefreshSessionLoop(authService: AuthService) { timeouts.push( setTimeout(async function refreshUser() { await authService.refreshUser(userId) + timeouts.push(setTimeout(refreshUser, 60_000)) }), )