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
15 changes: 14 additions & 1 deletion apps/cursor/src/app/auth/callback/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { NextResponse } from "next/server";
import { createClient } from "@/utils/supabase/server";

// `next` comes from the query string and is appended to a base URL, so values
// like `@evil.example` or `//evil.example` would otherwise leave the site.
function safeNextPath(next: string | null, origin: string): string {
if (!next) return "/";
try {
const url = new URL(next, origin);
if (url.origin !== origin) return "/";
return `${url.pathname}${url.search}${url.hash}`;
} catch {
return "/";
}
}

export async function GET(request: Request) {
const { searchParams, origin } = new URL(request.url);
const code = searchParams.get("code");
const next = searchParams.get("next") ?? "/";
const next = safeNextPath(searchParams.get("next"), origin);

if (code) {
const supabase = await createClient();
Expand Down
39 changes: 34 additions & 5 deletions apps/cursor/src/utils/supabase/auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
import { createClient } from "./server";

export async function getSession() {
export type VerifiedSession = {
user: {
id: string;
email: string | undefined;
user_metadata: Record<string, unknown>;
};
};

/**
* The signed-in user, read from the access token's verified claims.
*
* `supabase.auth.getSession()` returns whatever the session cookie contains
* without checking the token signature, so its `user` must never back an
* authorization decision on the server. `getClaims()` verifies the JWT
* (against the project's JWKS, or via the Auth server for symmetric keys)
* before we trust `sub`.
*/
export async function getSession(): Promise<VerifiedSession | null> {
const supabase = await createClient();
const {
data: { session },
} = await supabase.auth.getSession();
return session;
// getClaims throws, rather than returning an error, for an expired token.
const result = await supabase.auth.getClaims().catch(() => null);

if (!result || result.error || !result.data?.claims.sub) {
return null;
Comment thread
cursor[bot] marked this conversation as resolved.
}

const { claims } = result.data;

return {
user: {
id: claims.sub,
email: claims.email,
user_metadata: claims.user_metadata ?? {},
},
};
}
Loading