phase 1-6 implemented
This commit is contained in:
1 parent
22c274bb83
commit
2c4927478a
73 files changed
+9702
-2354
No files matched your search
@@ -1,4 +1,16 @@
|
||||
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
|
||||
import { validateEnv } from "./lib/env";
|
||||
import log from "./lib/logger";
|
||||
|
||||
// Validate environment variables on server startup
|
||||
try {
|
||||
validateEnv();
|
||||
log.info("Environment validation passed");
|
||||
} catch (error) {
|
||||
log.error("Environment validation failed", error);
|
||||
// Re-throw to prevent server from starting with invalid config
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Define routes that should be publicly accessible (no auth required)
|
||||
const isPublicRoute = createRouteMatcher([
|
||||
@@ -16,27 +28,26 @@ export default clerkMiddleware(async (auth, req) => {
|
||||
// Log for debugging
|
||||
const authHeader = req.headers.get("authorization");
|
||||
if (authHeader) {
|
||||
console.log(
|
||||
"[Middleware] Authorization header present:",
|
||||
authHeader.substring(0, 20) + "...",
|
||||
);
|
||||
log.debug("Authorization header present", {
|
||||
preview: authHeader.substring(0, 20) + "...",
|
||||
});
|
||||
}
|
||||
|
||||
// Don't protect public routes
|
||||
if (isPublicRoute(req)) {
|
||||
console.log("[Middleware] Public route, skipping auth");
|
||||
log.debug("Public route, skipping auth");
|
||||
return;
|
||||
}
|
||||
|
||||
// For API routes, let the route handler check auth
|
||||
// This allows API routes to handle both web sessions and mobile Bearer tokens
|
||||
if (isApiRoute(req)) {
|
||||
console.log("[Middleware] API route, auth will be checked in handler");
|
||||
log.debug("API route, auth will be checked in handler");
|
||||
return;
|
||||
}
|
||||
|
||||
// For all other routes (web pages), enforce authentication
|
||||
console.log("[Middleware] Protected route, requiring auth");
|
||||
log.debug("Protected route, requiring auth");
|
||||
await auth.protect();
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user