From 39cf4295f15ec842746d803914f7a8c06d7df4be Mon Sep 17 00:00:00 2001 From: Oksamies Date: Wed, 17 Sep 2025 20:55:59 +0300 Subject: [PATCH 1/2] Add forced currentUser update to settings and teams pages --- apps/cyberstorm-remix/app/root.tsx | 36 ++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/apps/cyberstorm-remix/app/root.tsx b/apps/cyberstorm-remix/app/root.tsx index 5cc7142ac..e8969b4ce 100644 --- a/apps/cyberstorm-remix/app/root.tsx +++ b/apps/cyberstorm-remix/app/root.tsx @@ -50,6 +50,7 @@ import { publicEnvVariablesType, } from "cyberstorm/security/publicEnvVariables"; import { StorageManager } from "@thunderstore/ts-api-react/src/storage"; +import { Route } from "./+types/root"; // REMIX TODO: https://remix.run/docs/en/main/route/links // export const links: LinksFunction = () => [{ rel: "stylesheet", href: styles }]; @@ -119,7 +120,7 @@ export async function loader() { }; } -export async function clientLoader() { +export async function clientLoader({ request }: Route.ClientLoaderArgs) { const publicEnvVariables = getPublicEnvVariables([ "VITE_SITE_URL", "VITE_BETA_SITE_URL", @@ -142,13 +143,28 @@ export async function clientLoader() { publicEnvVariables.VITE_COOKIE_DOMAIN ); - // We need to run this here too in addition to the, shouldRevalidate function, - // as for some reason the commits to localStorage are not done before the the clientLoader is run - sessionTools.sessionValid( - publicEnvVariables.VITE_API_URL, - publicEnvVariables.VITE_COOKIE_DOMAIN + let forceUpdateCurrentUser = false; + if ( + request.url.startsWith(`${publicEnvVariables.VITE_BETA_SITE_URL}/teams`) || + request.url.startsWith(`${publicEnvVariables.VITE_BETA_SITE_URL}/settings`) + ) { + forceUpdateCurrentUser = true; + } else { + // In all other cases check if actually need to fetch + // current-user data. Ideally we shouldn't need to do + // this sessionValid check again, but for some reason + // we need to run this here too in addition to the, + // shouldRevalidate function, cause for some reason + // the commits to localStorage are not done before + // the clientLoader is run. + sessionTools.sessionValid( + publicEnvVariables.VITE_API_URL, + publicEnvVariables.VITE_COOKIE_DOMAIN + ); + } + const currentUser = await sessionTools.getSessionCurrentUser( + forceUpdateCurrentUser ); - const currentUser = await sessionTools.getSessionCurrentUser(); const config = sessionTools.getConfig(publicEnvVariables.VITE_API_URL); return { publicEnvVariables: publicEnvVariables, @@ -164,11 +180,17 @@ export type RootLoadersType = typeof loader | typeof clientLoader; // this needs to be fixed, but it requires a more in-depth solution export function shouldRevalidate({ defaultShouldRevalidate, + nextUrl, }: ShouldRevalidateFunctionArgs) { const publicEnvVariables = getPublicEnvVariables([ "VITE_API_URL", "VITE_COOKIE_DOMAIN", ]); + if ( + nextUrl.pathname.startsWith("/teams") || + nextUrl.pathname.startsWith("/settings") + ) + return true; sessionValid( new StorageManager(SESSION_STORAGE_KEY), publicEnvVariables.VITE_API_URL || "", From 1537ac3f52bb3541dcd15933fe819a2d772bc745 Mon Sep 17 00:00:00 2001 From: Oksamies Date: Thu, 18 Sep 2025 19:05:06 +0300 Subject: [PATCH 2/2] Rename sessionValid to runSessionValidationCheck As the function has a side-effect on the localStorage values, the old name was misleading in it's effects --- apps/cyberstorm-remix/app/root.tsx | 8 ++++---- packages/ts-api-react/src/SessionContext.tsx | 15 +++++++++------ packages/ts-api-react/src/index.ts | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/apps/cyberstorm-remix/app/root.tsx b/apps/cyberstorm-remix/app/root.tsx index e8969b4ce..aff3784cd 100644 --- a/apps/cyberstorm-remix/app/root.tsx +++ b/apps/cyberstorm-remix/app/root.tsx @@ -43,7 +43,7 @@ import { getSessionContext, getSessionStale, SESSION_STORAGE_KEY, - sessionValid, + runSessionValidationCheck, } from "@thunderstore/ts-api-react/src/SessionContext"; import { getPublicEnvVariables, @@ -152,12 +152,12 @@ export async function clientLoader({ request }: Route.ClientLoaderArgs) { } else { // In all other cases check if actually need to fetch // current-user data. Ideally we shouldn't need to do - // this sessionValid check again, but for some reason + // this runSessionValidationCheck check again, but for some reason // we need to run this here too in addition to the, // shouldRevalidate function, cause for some reason // the commits to localStorage are not done before // the clientLoader is run. - sessionTools.sessionValid( + sessionTools.runSessionValidationCheck( publicEnvVariables.VITE_API_URL, publicEnvVariables.VITE_COOKIE_DOMAIN ); @@ -191,7 +191,7 @@ export function shouldRevalidate({ nextUrl.pathname.startsWith("/settings") ) return true; - sessionValid( + runSessionValidationCheck( new StorageManager(SESSION_STORAGE_KEY), publicEnvVariables.VITE_API_URL || "", publicEnvVariables.VITE_COOKIE_DOMAIN || "" diff --git a/packages/ts-api-react/src/SessionContext.tsx b/packages/ts-api-react/src/SessionContext.tsx index d38e61083..80f6a9148 100644 --- a/packages/ts-api-react/src/SessionContext.tsx +++ b/packages/ts-api-react/src/SessionContext.tsx @@ -26,7 +26,7 @@ export interface ContextInterface { /** Get RequestConfig for Dapper usage */ getConfig: (domain?: string) => RequestConfig; /** Check if session is valid and try to repair if not */ - sessionValid: (apiHost: string, cookieDomain: string) => boolean; + runSessionValidationCheck: (apiHost: string, cookieDomain: string) => boolean; /** apiHost of the session */ apiHost?: string; /** Async function to update currentUser */ @@ -120,7 +120,7 @@ function parseCurrentUser(currentUser: unknown): User { } } -export const sessionValid = ( +export const runSessionValidationCheck = ( _storage: StorageManager, apiHost: string, cookieDomain: string @@ -252,8 +252,11 @@ export const getSessionContext = ( }; // Check current session and try to fix it if cookies are not the same as storage - const _sessionValid = (apiHost: string, cookieDomain: string): boolean => { - return sessionValid(_storage, apiHost, cookieDomain); + const _runSessionValidationCheck = ( + apiHost: string, + cookieDomain: string + ): boolean => { + return runSessionValidationCheck(_storage, apiHost, cookieDomain); }; const _storeCurrentUser = (currentUser: User) => { @@ -279,7 +282,7 @@ export const getSessionContext = ( }); } else { _storage.setValue(API_HOST_KEY, apiHost); - _sessionValid(apiHost, cookieDomain); + _runSessionValidationCheck(apiHost, cookieDomain); } } @@ -287,7 +290,7 @@ export const getSessionContext = ( clearSession: _clearSession, clearCookies: _clearCookies, getConfig: _getConfig, - sessionValid: _sessionValid, + runSessionValidationCheck: _runSessionValidationCheck, updateCurrentUser: _updateCurrentUser, storeCurrentUser: _storeCurrentUser, getSessionCurrentUser: _getSessionCurrentUser, diff --git a/packages/ts-api-react/src/index.ts b/packages/ts-api-react/src/index.ts index 193377f0e..4c85be554 100644 --- a/packages/ts-api-react/src/index.ts +++ b/packages/ts-api-react/src/index.ts @@ -4,7 +4,7 @@ export { setSession, clearSession, getConfig, - sessionValid, + runSessionValidationCheck, storeCurrentUser, updateCurrentUser, getSessionCurrentUser,