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
29 changes: 15 additions & 14 deletions src/server/auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import {
SdkError
} from "../errors";
import { LogoutToken, SessionData, TokenSet } from "../types";
import {
ensureNoLeadingSlash,
ensureTrailingSlash,
removeTrailingSlash
} from "../utils/pathUtils";
import { toSafeRedirect } from "../utils/url-helpers";
import { AbstractSessionStore } from "./session/abstract-session-store";
import { TransactionState, TransactionStore } from "./transaction-store";
Expand Down Expand Up @@ -107,14 +112,6 @@ export interface AuthClientOptions {
enableTelemetry?: boolean;
}

function ensureTrailingSlash(value: string) {
return value && !value.endsWith('/') ? `${value}/` : value;
}

function ensureNoLeadingSlash(value: string) {
return value && value.startsWith('/') ? value.substring(1, value.length) : value;
}

function createRouteUrl(url: string, base: string) {
return new URL(ensureNoLeadingSlash(url), ensureTrailingSlash(base));
}
Expand Down Expand Up @@ -236,21 +233,25 @@ export class AuthClient {

async handler(req: NextRequest): Promise<NextResponse> {
const { pathname } = req.nextUrl;
const sanitizedPathname = removeTrailingSlash(pathname);
const method = req.method;

if (method === "GET" && pathname === this.routes.login) {
if (method === "GET" && sanitizedPathname === this.routes.login) {
return this.handleLogin(req);
} else if (method === "GET" && pathname === this.routes.logout) {
} else if (method === "GET" && sanitizedPathname === this.routes.logout) {
return this.handleLogout(req);
} else if (method === "GET" && pathname === this.routes.callback) {
} else if (method === "GET" && sanitizedPathname === this.routes.callback) {
return this.handleCallback(req);
} else if (method === "GET" && pathname === this.routes.profile) {
} else if (method === "GET" && sanitizedPathname === this.routes.profile) {
return this.handleProfile(req);
} else if (method === "GET" && pathname === this.routes.accessToken) {
} else if (
method === "GET" &&
sanitizedPathname === this.routes.accessToken
) {
return this.handleAccessToken(req);
} else if (
method === "POST" &&
pathname === this.routes.backChannelLogout
sanitizedPathname === this.routes.backChannelLogout
) {
return this.handleBackChannelLogout(req);
} else {
Expand Down
12 changes: 12 additions & 0 deletions src/utils/pathUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function ensureTrailingSlash(value: string) {
return value && !value.endsWith("/") ? `${value}/` : value;
}

export function ensureNoLeadingSlash(value: string) {
return value && value.startsWith("/")
? value.substring(1, value.length)
: value;
}

export const removeTrailingSlash = (path: string) =>
path.endsWith("/") ? path.slice(0, -1) : path;
Copy link
Contributor

@nandan-bhat nandan-bhat Mar 7, 2025

Choose a reason for hiding this comment

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

Please add test coverage to the above utility methods.