Skip to content
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

feat: SessionAuth SSR support (initialSessionAuthContext) #789

Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 15 additions & 10 deletions lib/build/index2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions lib/build/recipe/session/sessionAuth.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/build/recipe/session/types.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 19 additions & 5 deletions lib/ts/recipe/session/sessionAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import type { PropsWithChildren } from "react";
import type { ClaimValidationError } from "supertokens-web-js/recipe/session";

export type SessionAuthProps = {
/**
* Initial context that is rendered on a server side (SSR).
*/
initialSessionAuthContext?: SessionContextType;
sasha240100 marked this conversation as resolved.
Show resolved Hide resolved
/**
* For a detailed explanation please see https://github.com/supertokens/supertokens-auth-react/issues/570
*/
Expand Down Expand Up @@ -67,7 +71,7 @@ const SessionAuth: React.FC<PropsWithChildren<SessionAuthProps>> = ({ children,

// Reusing the parent context was removed because it caused a redirect loop in an edge case
// because it'd also reuse the invalid claims part until it loaded.
const [context, setContext] = useState<SessionContextType>({ loading: true });
const [context, setContext] = useState<SessionContextType>(props.initialSessionAuthContext ?? { loading: true });

const session = useRef<Session>();

Expand Down Expand Up @@ -101,6 +105,7 @@ const SessionAuth: React.FC<PropsWithChildren<SessionAuthProps>> = ({ children,

if (sessionExists === false) {
return {
isContextFromSSR: false,
loading: false,
doesSessionExist: false,
accessTokenPayload: {},
Expand Down Expand Up @@ -128,6 +133,7 @@ const SessionAuth: React.FC<PropsWithChildren<SessionAuthProps>> = ({ children,
throw err;
}
return {
isContextFromSSR: false,
loading: false,
doesSessionExist: false,
accessTokenPayload: {},
Expand All @@ -138,6 +144,7 @@ const SessionAuth: React.FC<PropsWithChildren<SessionAuthProps>> = ({ children,

try {
return {
isContextFromSSR: false,
loading: false,
doesSessionExist: true,
invalidClaims,
Expand All @@ -159,6 +166,7 @@ const SessionAuth: React.FC<PropsWithChildren<SessionAuthProps>> = ({ children,
// This means that loading the access token or the userId failed
// This may happen if the server cleared the error since the validation was done which should be extremely rare
return {
isContextFromSSR: false,
loading: false,
doesSessionExist: false,
accessTokenPayload: {},
Expand Down Expand Up @@ -246,7 +254,12 @@ const SessionAuth: React.FC<PropsWithChildren<SessionAuthProps>> = ({ children,
userContext,
});
if (failureRedirectInfo.redirectPath) {
setContext({ ...event.sessionContext, loading: false, invalidClaims });
setContext({
...event.sessionContext,
isContextFromSSR: false,
loading: false,
invalidClaims,
});
return await SuperTokens.getInstanceOrThrow().redirectToUrl(
failureRedirectInfo.redirectPath,
navigate
Expand All @@ -259,21 +272,22 @@ const SessionAuth: React.FC<PropsWithChildren<SessionAuthProps>> = ({ children,
});
return setContext({
...event.sessionContext,
isContextFromSSR: false,
loading: false,
invalidClaims,
accessDeniedValidatorError: failureRedirectInfo.failedClaim,
});
}
}
setContext({ ...event.sessionContext, loading: false, invalidClaims });
setContext({ ...event.sessionContext, isContextFromSSR: false, loading: false, invalidClaims });

return;
}
case "SIGN_OUT":
setContext({ ...event.sessionContext, loading: false, invalidClaims: [] });
setContext({ ...event.sessionContext, isContextFromSSR: false, loading: false, invalidClaims: [] });
return;
case "UNAUTHORISED":
setContext({ ...event.sessionContext, loading: false, invalidClaims: [] });
setContext({ ...event.sessionContext, isContextFromSSR: false, loading: false, invalidClaims: [] });
if (props.onSessionExpired !== undefined) {
props.onSessionExpired();
} else if (props.requireAuth !== false && props.doRedirection !== false) {
Expand Down
1 change: 1 addition & 0 deletions lib/ts/recipe/session/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type SessionContextUpdate = {
};

export type LoadedSessionContext = {
isContextFromSSR: boolean;
loading: false;
invalidClaims: ClaimValidationError[];
accessDeniedValidatorError?: ClaimValidationError;
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/unit/recipe/session/sessionAuth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ describe("SessionAuth", () => {
doesSessionExist: true,
invalidClaims: [],
loading: false,
isContextFromSSR: false,
});
});

Expand Down Expand Up @@ -216,6 +217,7 @@ describe("SessionAuth", () => {
userId: "mock-id",
invalidClaims: [],
loading: false,
isContextFromSSR: false,
});

// when
Expand Down Expand Up @@ -508,6 +510,7 @@ describe("SessionAuth", () => {
userId: "mock-id",
invalidClaims: [{ validatorId: "st-test-claim", reason: "test-reason" }],
loading: false,
isContextFromSSR: false,
});

await act(() =>
Expand Down Expand Up @@ -664,6 +667,7 @@ describe("SessionAuth", () => {
userId: "",
invalidClaims: [],
loading: false,
isContextFromSSR: false,
});

// when
Expand Down Expand Up @@ -692,6 +696,7 @@ describe("SessionAuth", () => {
userId: "",
invalidClaims: [],
loading: false,
isContextFromSSR: false,
});

// when
Expand Down