-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
45 lines (37 loc) · 1.2 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
import { NextResponse, NextRequest } from "next/server";
import { getToken } from "next-auth/jwt";
export { default } from "next-auth/middleware";
export async function middleware(request: NextRequest) {
// Log request URL
console.log("Request URL:", request.url);
// Attempt to retrieve the token
const token = await getToken({ req: request });
// Log token details
console.log("Token:", token);
const url = request.nextUrl;
if (!token && url.pathname.startsWith("/board")) {
console.log("No token found, redirecting to /auth/sign-in");
return NextResponse.redirect(new URL("/auth/sign-in", request.url));
}
if (
token &&
(url.pathname.startsWith("/auth/sign-in") ||
url.pathname.startsWith("/auth/sign-up") ||
url.pathname === "/")
) {
console.log("User is authenticated, redirecting to /board");
return NextResponse.redirect(new URL("/board", request.url));
}
// If no conditions are met, allow request to proceed
console.log("Proceeding to the requested URL");
return NextResponse.next();
}
export const config = {
matcher: [
"/",
"/board/:path*",
"/auth/sign-in",
"/auth/sign-up",
"/tasks/:path*",
], // Protect these routes
};