Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions apps/cyberstorm-remix/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }];
Expand Down Expand Up @@ -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",
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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 forceUpdateCurrentUser variable here or argument in getSessionCurrentUser. I.e. simpler but does the same thing (if I'm reading the code right).

} 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,
Expand All @@ -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 || ""
Expand Down
15 changes: 9 additions & 6 deletions packages/ts-api-react/src/SessionContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -120,7 +120,7 @@ function parseCurrentUser(currentUser: unknown): User {
}
}

export const sessionValid = (
export const runSessionValidationCheck = (
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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) => {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-api-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export {
setSession,
clearSession,
getConfig,
sessionValid,
runSessionValidationCheck,
storeCurrentUser,
updateCurrentUser,
getSessionCurrentUser,
Expand Down
Loading