forked from composecraft/composecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
90 lines (79 loc) · 3.29 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
87
88
89
90
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { cookies } from 'next/headers';
import { jwtVerify } from 'jose'; // Importing the verification function
function isCoreProductPage(path:string):boolean{
if(path.startsWith("/library")){
console.log("non core product page triggered redirect to /")
return false
}else if(path.startsWith("/docs")){
console.log("non core product page triggered redirect to /")
return false
}
return true
}
export async function middleware(req: NextRequest) {
if(process.env.CORE_ONLY || false){
if(!isCoreProductPage(req.nextUrl.pathname)){
return NextResponse.redirect(new URL("/", req.url));
}
}
// Apply checkAuth to routes under /dashboard
if (req.nextUrl.pathname.includes("/dashboard")) {
const rawToken = cookies().get("token")?.value;
try {
if (!rawToken) {
return NextResponse.redirect(new URL("/", req.url));
}
// Secret key needs to be a Uint8Array for jose
const secretKey = new TextEncoder().encode(process.env.SECRET_KEY || "");
// Verify the token using jose's jwtVerify method
// eslint-disable-next-line @typescript-eslint/no-unused-vars
await jwtVerify(rawToken, secretKey);
// You can access the decoded token payload
//console.log(payload);
} catch (error) {
console.error(error);
return NextResponse.redirect(new URL("/", req.url));
}
}else if(req.nextUrl.pathname.includes("/login") && !req.nextUrl.pathname.includes("/cli")){
const rawToken = cookies().get("token")?.value;
try {
if(rawToken){
const secretKey = new TextEncoder().encode(process.env.SECRET_KEY || "");
await jwtVerify(rawToken, secretKey);
return NextResponse.redirect(new URL("/dashboard", req.url));
}
} catch (error) {
console.error(error);
return NextResponse.redirect(new URL("/", req.url));
}
}else if(req.nextUrl.pathname.includes("/tryIt")){
const rawToken = cookies().get("token")?.value;
try {
if(rawToken){
const secretKey = new TextEncoder().encode(process.env.SECRET_KEY || "");
await jwtVerify(rawToken, secretKey);
return NextResponse.redirect(new URL("/dashboard/playground", req.url));
}
} catch (error) {
console.error(error);
return NextResponse.redirect(new URL("/", req.url));
}
}
else if(req.nextUrl.pathname.includes("/signin")){
const rawToken = cookies().get("token")?.value;
try {
if(rawToken){
const secretKey = new TextEncoder().encode(process.env.SECRET_KEY || "");
await jwtVerify(rawToken, secretKey);
return NextResponse.redirect(new URL("/dashboard", req.url));
}
} catch (error) {
console.error(error);
return NextResponse.redirect(new URL("/", req.url));
}
}
// If authenticated or the route is not under /dashboard, continue the request
return NextResponse.next();
}