-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy.ts
More file actions
59 lines (52 loc) · 1.73 KB
/
Copy pathproxy.ts
File metadata and controls
59 lines (52 loc) · 1.73 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { getIronSession } from "iron-session";
import { cookies } from "next/headers";
import { env } from "@/env";
export async function proxy(request: NextRequest) {
const { pathname, searchParams } = request.nextUrl;
// Protect admin routes
if (pathname.includes("/(admin)") || pathname.includes("/editor")) {
const session = await getIronSession<{
githubUsername?: string;
}>(await cookies(), {
cookieName: "auth",
password: env.GITHUB_CLIENT_SECRET,
cookieOptions: {
secure: env.NODE_ENV === "production",
httpOnly: true,
path: "/",
maxAge: 60 * 60 * 24 * 365,
},
});
// Check if user is authenticated and is admin
if (!session.githubUsername || session.githubUsername !== "jokull") {
const currentUrl = pathname + (searchParams.toString() ? `?${searchParams}` : "");
// In development, redirect to dev auth
if (env.NODE_ENV === "development") {
const url = request.nextUrl.clone();
url.pathname = "/api/dev-auth";
url.searchParams.set("next", currentUrl);
return NextResponse.redirect(url);
}
// In production, redirect to GitHub OAuth
const url = request.nextUrl.clone();
url.pathname = "/auth/login";
url.searchParams.set("next", currentUrl);
return NextResponse.redirect(url);
}
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
*/
"/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
],
};