routes fix

extensive loging added
This commit is contained in:
dimitar committed 2026-03-10 02:22:11 +01:00
1 parent 36315ea5cf
commit 22c274bb83
9 files changed
+1252 -508

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
import { auth, currentUser } from "@clerk/nextjs/server";
import { NextRequest } from "next/server";
/**
* Get authenticated user ID from request
* Handles both session-based auth (web) and Bearer token auth (mobile)
*
* For mobile apps using Clerk Expo, tokens should be passed as:
* Authorization: Bearer <token>
*/
export async function getAuthUserId(req: NextRequest): Promise<string | null> {
try {
// Clerk's auth() should handle both cookies and Bearer tokens automatically
// when the request is properly formatted
const { userId } = await auth();
if (userId) {
console.log("✓ Authenticated user:", userId);
return userId;
}
console.log("✗ No authentication found");
// Log headers for debugging
const authHeader = req.headers.get("authorization");
console.log("Authorization header:", authHeader ? "Present" : "Missing");
return null;
} catch (error) {
console.error("Authentication error:", error);
return null;
}
}
/**
* Simplified version that just uses Clerk's built-in auth
* This should work with both session cookies and Bearer tokens
*/
export async function requireAuth(req: NextRequest): Promise<string> {
const userId = await getAuthUserId(req);
if (!userId) {
throw new Error("Unauthorized");
}
return userId;
}