Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.

Commit c96a906

Browse files
committed
feat: archiving project redirect to github profile
1 parent d88563f commit c96a906

File tree

1 file changed

+2
-98
lines changed

1 file changed

+2
-98
lines changed

middleware.js

+2-98
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,13 @@
1-
import { getToken } from "next-auth/jwt";
21
import { NextResponse } from "next/server";
32

43
// note: logger is not available in middleware, using console.log instead
54

65
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*"],
187
};
198

209
export async function middleware(req) {
21-
const protocol = process.env.NODE_ENV === "development" ? "http" : "https";
22-
const hostname = req.headers.get("host");
2310
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-
}
96-
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-
}
106-
}
10711

108-
return NextResponse.next();
12+
return NextResponse.redirect(new URL(reqPathName, "https://github.com"));
10913
}

0 commit comments

Comments
 (0)