-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
43 lines (38 loc) · 1.3 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
import { NextRequest, NextResponse } from "next/server";
export async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
const httpMethod = req.method;
const authToken =
req.cookies.get("next-auth.session-token")?.value ??
req.cookies.get("__Secure-next-auth.session-token")?.value;
if (!authToken) {
if (pathname === "/dashboard") {
return NextResponse.redirect(new URL("/dashboard/login", req.url));
} else if (pathname === "/api/project" && httpMethod !== "GET") {
return new NextResponse(JSON.stringify({ error: "Unauthorized!" }), {
status: 401,
});
} else if (pathname === "/api/experience" && httpMethod !== "GET") {
return new NextResponse(JSON.stringify({ error: "Unauthorized!" }), {
status: 401,
});
} else if (pathname === "/api/auth/signup") {
return new NextResponse(
JSON.stringify({
error: "Unauthorized! Only existing admin can add new admin",
}),
{
status: 401,
}
);
}
} else {
if (pathname === "/dashboard/login") {
return NextResponse.redirect(new URL("/dashboard", req.url));
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*", "/api/project/:path*", "/api/auth/:path*"],
};