-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
43 lines (33 loc) · 1.34 KB
/
middleware.ts
File metadata and controls
43 lines (33 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { auth } from "@/lib/auth";
import { NextResponse } from "next/server";
import { DEMO_CONFIG } from "@/lib/demo-config";
const PAT_COOKIE_NAME = "github_pat";
export default auth((req) => {
const isOnApi = req.nextUrl.pathname.startsWith("/api");
// Skip middleware for API routes - let them handle their own auth
if (isOnApi) {
return;
}
// Check for OAuth session OR PAT cookie
const hasOAuthSession = !!req.auth;
const hasPATCookie = req.cookies.has(PAT_COOKIE_NAME);
const isLoggedIn = hasOAuthSession || hasPATCookie;
const isOnWrapped = req.nextUrl.pathname.startsWith("/wrapped");
const isOnLogin = req.nextUrl.pathname === "/login";
// Check if this is the demo repo path
// Match /wrapped/vercel/swr (case-insensitive)
const isDemoPath = req.nextUrl.pathname === DEMO_CONFIG.path ||
req.nextUrl.pathname.toLowerCase() === DEMO_CONFIG.path.toLowerCase();
// Protect wrapped routes - require authentication (except for demo)
if (isOnWrapped && !isLoggedIn && !isDemoPath) {
return Response.redirect(new URL("/login", req.url));
}
// Redirect to home if already logged in and on login page
if (isOnLogin && isLoggedIn) {
return Response.redirect(new URL("/", req.url));
}
return;
});
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico|.*\\..*).*)"],
};