fix #12: handle __Secure- cookie prefix in all auth bypass code
Better Auth sets cookies with __Secure- prefix when served over HTTPS. Updated cookie parsing in compare, user/comparisons, and user/stats routes to check for both __Secure-better-auth.session_token and better-auth.session_token.
This commit is contained in:
@@ -26,10 +26,11 @@ export async function POST(request: Request) {
|
|||||||
// Bypass auth.api.getSession() — Drizzle queryWithCache bug (#12)
|
// Bypass auth.api.getSession() — Drizzle queryWithCache bug (#12)
|
||||||
// Manually parse session token from cookie and query sessions table directly
|
// Manually parse session token from cookie and query sessions table directly
|
||||||
const cookieHeader = request.headers.get("cookie") ?? "";
|
const cookieHeader = request.headers.get("cookie") ?? "";
|
||||||
const tokenMatch = cookieHeader
|
const cookieMatch = cookieHeader
|
||||||
.split(";")
|
.split(";")
|
||||||
.find((c) => c.trim().startsWith("better-auth.session_token="));
|
.map((c) => c.trim())
|
||||||
const token = tokenMatch?.split("=")?.[1]?.trim();
|
.find((c) => c.startsWith("__Secure-better-auth.session_token=") || c.startsWith("better-auth.session_token="));
|
||||||
|
const token = cookieMatch?.split("=")?.slice(1)?.join("=")?.trim();
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return Response.json({ error: "Authentication required" }, { status: 401 });
|
return Response.json({ error: "Authentication required" }, { status: 401 });
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ export async function GET(request: Request) {
|
|||||||
// Bypass auth.api.getSession() — Drizzle queryWithCache bug (#12)
|
// Bypass auth.api.getSession() — Drizzle queryWithCache bug (#12)
|
||||||
const hdrs = await headers();
|
const hdrs = await headers();
|
||||||
const cookieHeader = hdrs.get("cookie") ?? "";
|
const cookieHeader = hdrs.get("cookie") ?? "";
|
||||||
const tokenMatch = cookieHeader
|
const cookieMatch = cookieHeader
|
||||||
.split(";")
|
.split(";")
|
||||||
.find((c) => c.trim().startsWith("better-auth.session_token="));
|
.map((c) => c.trim())
|
||||||
const token = tokenMatch?.split("=")?.[1]?.trim();
|
.find((c) => c.startsWith("__Secure-better-auth.session_token=") || c.startsWith("better-auth.session_token="));
|
||||||
|
const token = cookieMatch?.split("=")?.slice(1)?.join("=")?.trim();
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ export async function GET() {
|
|||||||
// Bypass auth.api.getSession() — Drizzle queryWithCache bug (#12)
|
// Bypass auth.api.getSession() — Drizzle queryWithCache bug (#12)
|
||||||
const hdrs = await headers();
|
const hdrs = await headers();
|
||||||
const cookieHeader = hdrs.get("cookie") ?? "";
|
const cookieHeader = hdrs.get("cookie") ?? "";
|
||||||
const tokenMatch = cookieHeader
|
const cookieMatch = cookieHeader
|
||||||
.split(";")
|
.split(";")
|
||||||
.find((c) => c.trim().startsWith("better-auth.session_token="));
|
.map((c) => c.trim())
|
||||||
const token = tokenMatch?.split("=")?.[1]?.trim();
|
.find((c) => c.startsWith("__Secure-better-auth.session_token=") || c.startsWith("better-auth.session_token="));
|
||||||
|
const token = cookieMatch?.split("=")?.slice(1)?.join("=")?.trim();
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user