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: Add getInitialSessionAuthContext to compute SessionAuth context #785

Closed
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions lib/build/nextjs.d.ts

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

19 changes: 18 additions & 1 deletion lib/build/nextjs.js

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

10 changes: 10 additions & 0 deletions lib/build/types.d.ts

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

22 changes: 21 additions & 1 deletion lib/ts/nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
middleware,
errorHandler as customErrorHandler,
} from "./framework/custom";
import { HTTPMethod } from "./types";
import { HTTPMethod, SSRSessionContextType } from "./types";
import Session, { SessionContainer, VerifySessionOptions } from "./recipe/session";
import SessionRecipe from "./recipe/session/recipe";
import { getToken } from "./recipe/session/cookieAndHeaders";
Expand Down Expand Up @@ -230,6 +230,25 @@ export default class NextJS {
return result;
}

static async getInitialSessionAuthContext(session: SessionContainer | undefined): Promise<SSRSessionContextType> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this function. And if we are going to keep it, why is it in the nextjs file?

const initialContext: SSRSessionContextType = {
isContextFromSSR: true,
loading: false,
doesSessionExist: false,
accessTokenPayload: {},
invalidClaims: [],
userId: "",
};

if (session) {
initialContext.doesSessionExist = true;
initialContext.accessTokenPayload = await session.getAccessTokenPayload();
initialContext.userId = await session.getUserId();
}

return initialContext;
}

static async withSession<NextRequest extends PartialNextRequest, NextResponse extends Response>(
req: NextRequest,
handler: (error: Error | undefined, session: SessionContainer | undefined) => Promise<NextResponse>,
Expand Down Expand Up @@ -405,5 +424,6 @@ export default class NextJS {
export let superTokensNextWrapper = NextJS.superTokensNextWrapper;
export let getAppDirRequestHandler = NextJS.getAppDirRequestHandler;
export let getSSRSession = NextJS.getSSRSession;
export let getInitialSessionAuthContext = NextJS.getInitialSessionAuthContext;
export let withSession = NextJS.withSession;
export let withPreParsedRequestResponse = NextJS.withPreParsedRequestResponse;
11 changes: 11 additions & 0 deletions lib/ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import NormalisedURLDomain from "./normalisedURLDomain";
import NormalisedURLPath from "./normalisedURLPath";
import { TypeFramework } from "./framework/types";
import { RecipeLevelUser } from "./recipe/accountlinking/types";
import { ClaimValidationError } from "./recipe/session/types";
import { BaseRequest } from "./framework";

export type AppInfo = {
Expand Down Expand Up @@ -113,3 +114,13 @@ export type User = {
// the recipeUserId can be converted to string from the RecipeUserId object type.
toJson: () => any;
};

export type SSRSessionContextType = {
isContextFromSSR: true;
loading: false;
Comment on lines +118 to +119
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should not be required since they are static values.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But they are expected by supertokens-auth-react, if they will be optional - that means we would have to polyfill them inside SessionAuth component, and in that case I would rather remove them completely from supertokens-node

doesSessionExist: boolean;
accessTokenPayload: any;
invalidClaims: ClaimValidationError[]; // TODO: Find if it's possible to compute invalidClaims on BE
sasha240100 marked this conversation as resolved.
Show resolved Hide resolved
userId?: string;
accessDeniedValidatorError?: ClaimValidationError;
};