Skip to content

Commit

Permalink
Reuse even more code from customframework into next.js
Browse files Browse the repository at this point in the history
  • Loading branch information
deepjyoti30-st committed Oct 3, 2024
1 parent 88f9500 commit 54225ab
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 139 deletions.
12 changes: 11 additions & 1 deletion lib/build/customFramework.d.ts

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

3 changes: 2 additions & 1 deletion lib/build/customFramework.js

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

1 change: 0 additions & 1 deletion lib/build/nextjs.d.ts

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

68 changes: 6 additions & 62 deletions lib/build/nextjs.js

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

6 changes: 2 additions & 4 deletions lib/ts/customFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import { getToken } from "./recipe/session/cookieAndHeaders";
import { parseJWTWithoutSignatureVerification } from "./recipe/session/jwt";
import { jwtVerify, JWTPayload, createRemoteJWKSet } from "jose";
import SuperTokens from "./supertokens";

// Define supported types for HTTPMethod
export type HTTPMethod = "post" | "get" | "delete" | "put" | "options" | "trace";
import { HTTPMethod } from "./types";

export type GetCookieFn<T extends ParsableRequest = Request> = (req: T) => Record<string, string>;

Expand Down Expand Up @@ -138,7 +136,7 @@ export function handleAuthAPIRequest(CustomResponse: typeof Response) {
return getHandleCall<Request>(CustomResponse, stMiddleware);
}

async function getSessionDetails(
export async function getSessionDetails(
preParsedRequest: PreParsedRequest,
options?: VerifySessionOptions,
userContext?: Record<string, unknown>
Expand Down
84 changes: 14 additions & 70 deletions lib/ts/nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ import {
middleware,
errorHandler as customErrorHandler,
} from "./framework/custom";
import { HTTPMethod, UserContext } from "./types";
import Session, { SessionContainer, VerifySessionOptions } from "./recipe/session";
import SessionRecipe from "./recipe/session/recipe";
import { getToken } from "./recipe/session/cookieAndHeaders";
import { availableTokenTransferMethods } from "./recipe/session/constants";
import { parseJWTWithoutSignatureVerification } from "./recipe/session/jwt";
import { addCookies, createPreParsedRequest, GetCookieFn, getHandleCall, handleError } from "./customFramework";
import { HTTPMethod } from "./types";
import { SessionContainer, VerifySessionOptions } from "./recipe/session";
import {
addCookies,
createPreParsedRequest,
GetCookieFn,
getHandleCall,
getSessionDetails,
handleError,
} from "./customFramework";

function next(
request: any,
Expand Down Expand Up @@ -106,65 +109,6 @@ export default class NextJS {
return getHandleCall<T>(NextResponse, stMiddleware);
}

private static async commonSSRSession(
baseRequest: PreParsedRequest,
options: VerifySessionOptions | undefined,
userContext: UserContext
): Promise<{
session: SessionContainer | undefined;
hasToken: boolean;
hasInvalidClaims: boolean;
baseResponse: CollectingResponse;
nextResponse?: Response;
}> {
let baseResponse = new CollectingResponse();

const recipe = SessionRecipe.getInstanceOrThrowError();
const tokenTransferMethod = recipe.config.getTokenTransferMethod({
req: baseRequest,
forCreateNewSession: false,
userContext,
});
const transferMethods = tokenTransferMethod === "any" ? availableTokenTransferMethods : [tokenTransferMethod];
const hasToken = transferMethods.some((transferMethod) => {
const token = getToken(baseRequest, "access", transferMethod);
if (!token) {
return false;
}

try {
parseJWTWithoutSignatureVerification(token);
return true;
} catch {
return false;
}
});

try {
let session = await Session.getSession(baseRequest, baseResponse, options, userContext);
return {
session,
hasInvalidClaims: false,
hasToken,
baseResponse,
};
} catch (err) {
if (Session.Error.isErrorFromSuperTokens(err)) {
return {
hasToken,
hasInvalidClaims: err.type === Session.Error.INVALID_CLAIMS,
session: undefined,
baseResponse,
nextResponse: new Response("Authentication required", {
status: err.type === Session.Error.INVALID_CLAIMS ? 403 : 401,
}),
};
} else {
throw err;
}
}
}

static async getSSRSession(
cookies: Array<{ name: string; value: string }>,
headers: Headers,
Expand All @@ -189,7 +133,7 @@ export default class NextJS {
getJSONBody: async () => [],
});

const { baseResponse, nextResponse, ...result } = await NextJS.commonSSRSession(
const { baseResponse, response, ...result } = await getSessionDetails(
baseRequest,
options,
getUserContext(userContext)
Expand Down Expand Up @@ -219,14 +163,14 @@ export default class NextJS {
getJSONBody: () => req!.json(),
});

const { session, nextResponse, baseResponse } = await NextJS.commonSSRSession(
const { session, response, baseResponse } = await getSessionDetails(
baseRequest,
options,
getUserContext(userContext)
);

if (nextResponse) {
return nextResponse as NextResponse;
if (response) {
return response as NextResponse;
}

let userResponse: NextResponse;
Expand Down

0 comments on commit 54225ab

Please sign in to comment.