|
1 | | -import { getToken } from "next-auth/jwt"; |
2 | 1 | import { NextResponse } from "next/server"; |
3 | 2 |
|
4 | 3 | // note: logger is not available in middleware, using console.log instead |
5 | 4 |
|
6 | 5 | export const config = { |
7 | | - matcher: [ |
8 | | - "/", |
9 | | - |
10 | | - // account management |
11 | | - "/account/:path*", |
12 | | - "/api/account/:path*", |
13 | | - |
14 | | - // admin section |
15 | | - "/admin/:path*", |
16 | | - "/api/admin/:path*", |
17 | | - ], |
| 6 | + matcher: ["/:path*"], |
18 | 7 | }; |
19 | 8 |
|
20 | 9 | export async function middleware(req) { |
21 | | - const protocol = process.env.NODE_ENV === "development" ? "http" : "https"; |
22 | | - const hostname = req.headers.get("host"); |
23 | | - const reqPathName = req.nextUrl.pathname; |
24 | | - const sessionRequired = ["/account", "/api/account"]; |
25 | | - const adminRequired = ["/admin", "/api/admin"]; |
26 | | - const adminUsers = process.env.ADMIN_USERS.split(","); |
27 | | - const hostedDomain = process.env.NEXT_PUBLIC_BASE_URL.replace( |
28 | | - /http:\/\/|https:\/\//, |
29 | | - "", |
30 | | - ); |
31 | | - const hostedDomains = [hostedDomain, `www.${hostedDomain}`]; |
32 | | - |
33 | | - // if custom domain + on root path |
34 | | - if (!hostedDomains.includes(hostname) && reqPathName === "/") { |
35 | | - console.log(`custom domain used: "${hostname}"`); |
36 | | - |
37 | | - let res; |
38 | | - let profile; |
39 | | - let url = `${ |
40 | | - process.env.NEXT_PUBLIC_BASE_URL |
41 | | - }/api/search/${encodeURIComponent(hostname)}`; |
42 | | - try { |
43 | | - res = await fetch(url, { |
44 | | - method: "GET", |
45 | | - headers: { |
46 | | - "Content-Type": "application/json", |
47 | | - }, |
48 | | - }); |
49 | | - profile = await res.json(); |
50 | | - } catch (e) { |
51 | | - console.error(url, e); |
52 | | - return NextResponse.error(e); |
53 | | - } |
54 | | - |
55 | | - if ( |
56 | | - profile?.username && |
57 | | - profile.settings?.domain && |
58 | | - profile.settings.domain === hostname |
59 | | - ) { |
60 | | - console.log( |
61 | | - `custom domain matched "${hostname}" for username "${profile.username}" (protocol: "${protocol}")`, |
62 | | - ); |
63 | | - // if match found rewrite to custom domain and display profile page |
64 | | - return NextResponse.rewrite( |
65 | | - new URL( |
66 | | - `/${profile.username}`, |
67 | | - `${protocol}://${profile.settings.domain}`, |
68 | | - ), |
69 | | - ); |
70 | | - } |
71 | | - |
72 | | - console.error(`custom domain NOT matched "${hostname}"`); |
73 | | - } |
74 | | - |
75 | | - // if not in sessionRequired or adminRequired, skip |
76 | | - if ( |
77 | | - !sessionRequired |
78 | | - .concat(adminRequired) |
79 | | - .some((path) => reqPathName.startsWith(path)) |
80 | | - ) { |
81 | | - return NextResponse.next(); |
82 | | - } |
83 | | - |
84 | | - const session = await getToken({ |
85 | | - req: req, |
86 | | - secret: process.env.NEXTAUTH_SECRET, |
87 | | - }); |
88 | | - |
89 | | - // if no session reject request |
90 | | - if (!session) { |
91 | | - if (reqPathName.startsWith("/api")) { |
92 | | - return NextResponse.json({}, { status: 401 }); |
93 | | - } |
94 | | - return NextResponse.redirect(new URL("/auth/signin", req.url)); |
95 | | - } |
| 10 | + const path = req.nextUrl.pathname; |
96 | 11 |
|
97 | | - const username = session.username; |
98 | | - // if admin request check user is allowed |
99 | | - if (adminRequired.some((path) => reqPathName.startsWith(path))) { |
100 | | - if (!adminUsers.includes(username)) { |
101 | | - if (reqPathName.startsWith("/api")) { |
102 | | - return NextResponse.json({}, { status: 401 }); |
103 | | - } |
104 | | - return NextResponse.redirect(new URL("/404", req.url)); |
105 | | - } |
| 12 | + if (path !== "/") { |
| 13 | + return NextResponse.redirect(new URL(path, "https://github.com")); |
106 | 14 | } |
107 | 15 |
|
108 | 16 | return NextResponse.next(); |
|
0 commit comments