-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmiddleware.ts
86 lines (72 loc) · 2.76 KB
/
middleware.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { jwtVerify } from "jose";
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api)(.*)"],
};
const allowedAPIPaths = ["/api/auth", "/api/invites"];
export async function middleware(req: NextRequest) {
const requestHeaders = new Headers(req.headers);
const token = req.headers.get("Authorization")?.split(" ")[1];
const refreshToken = req.cookies.get("token")?.value;
const { pathname } = req.nextUrl;
// Return early with 200 for CORS preflight
if (req.method === "OPTIONS") {
return new NextResponse(null, {
status: 200,
});
}
if (pathname.startsWith("/api")) {
// allowedAPIPaths.forEach((path) => {
// if (pathname.startsWith(path)) {
// return NextResponse.next();
// }
// });
if (pathname.startsWith("/api/auth")) return NextResponse.next();
if (!token) {
return new NextResponse(null, {
status: 401,
});
} else {
try {
const secret = new TextEncoder().encode(process.env.ACCESS_TOKEN_SECRET);
const { payload } = await jwtVerify(token, secret, {
issuer: process.env.ISSUER,
audience: process.env.AUDIENCE,
});
if (!payload.id) {
return new NextResponse(null, {
status: 401,
});
}
requestHeaders.set("X-UserId", payload.id as string);
return NextResponse.next({
request: {
headers: requestHeaders,
},
});
} catch (error) {
console.log(error);
return new NextResponse(null, {
status: 401,
});
}
}
}
const paths = ["/", "/login", "/register", "/download", "/channels/me", "/channels/discover"];
const regex = [/^\/channels\/me\/[0-9a-f]{24}\/?$/, /^\/channels\/[0-9a-f]{24}(\/[0-9a-f]{24})?\/?$/];
if (!paths.includes(pathname) && !regex.some((r) => pathname.match(r))) {
if (!refreshToken) {
if (pathname.startsWith("/channels")) {
return NextResponse.redirect(new URL("/login", req.url));
}
return NextResponse.redirect(new URL("/", req.url));
} else {
if (pathname.startsWith("/channels")) {
return NextResponse.redirect(new URL("/channels/me", req.url));
}
return NextResponse.redirect(new URL("/", req.url));
}
}
return NextResponse.next();
}