-
Couldn't load subscription status.
- Fork 5
Add forced currentUser update to settings and teams pages #1535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,13 +43,14 @@ import { | |
| getSessionContext, | ||
| getSessionStale, | ||
| SESSION_STORAGE_KEY, | ||
| sessionValid, | ||
| runSessionValidationCheck, | ||
| } from "@thunderstore/ts-api-react/src/SessionContext"; | ||
| import { | ||
| getPublicEnvVariables, | ||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: IMO we could just as well call sessionTools.setSessionStale() here? Wouldn't require |
||
| } else { | ||
| // In all other cases check if actually need to fetch | ||
| // current-user data. Ideally we shouldn't need to do | ||
| // 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.runSessionValidationCheck( | ||
| 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,12 +180,18 @@ 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", | ||
| ]); | ||
| sessionValid( | ||
| if ( | ||
| nextUrl.pathname.startsWith("/teams") || | ||
| nextUrl.pathname.startsWith("/settings") | ||
| ) | ||
| return true; | ||
| runSessionValidationCheck( | ||
| new StorageManager(SESSION_STORAGE_KEY), | ||
| publicEnvVariables.VITE_API_URL || "", | ||
| publicEnvVariables.VITE_COOKIE_DOMAIN || "" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = ( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: I think this shouldn't return boolean as the value is never used for anything and removing it would simplify the implementation. |
||
| _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,15 +282,15 @@ export const getSessionContext = ( | |
| }); | ||
| } else { | ||
| _storage.setValue(API_HOST_KEY, apiHost); | ||
| _sessionValid(apiHost, cookieDomain); | ||
| _runSessionValidationCheck(apiHost, cookieDomain); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| clearSession: _clearSession, | ||
| clearCookies: _clearCookies, | ||
| getConfig: _getConfig, | ||
| sessionValid: _sessionValid, | ||
| runSessionValidationCheck: _runSessionValidationCheck, | ||
| updateCurrentUser: _updateCurrentUser, | ||
| storeCurrentUser: _storeCurrentUser, | ||
| getSessionCurrentUser: _getSessionCurrentUser, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.