-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
53 lines (44 loc) · 1.31 KB
/
middleware.js
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
import {
DEFAULT_LOGIN_REDIRECT,
apiAuthPrefix,
authRoutes,
publicRoutes,
} from "@/routes";
import { NextResponse } from "next/server";
export default function middleware(req) {
const { pathname } = req.nextUrl;
//setting params
const searchParams = req.nextUrl.searchParams;
const params = new URLSearchParams(searchParams.toString());
const requestHeaders = new Headers(req.headers);
requestHeaders.set("params", params.get("season") ?? "1");
const token =
req.cookies.get("next-auth.session-token") ||
req.cookies.get("__Secure-next-auth.session-token");
const isApiAuthRoute = pathname.startsWith(apiAuthPrefix);
const isPublicRoute = publicRoutes.includes(pathname);
const isAuthRoute = authRoutes.includes(pathname);
if (isApiAuthRoute) {
return null;
}
if (isAuthRoute) {
if (token) {
return NextResponse.redirect(
new URL(DEFAULT_LOGIN_REDIRECT, req.nextUrl)
);
}
return null;
}
if (!token && !isPublicRoute) {
return NextResponse.redirect(new URL("/signin", req.nextUrl));
}
return NextResponse.next({
request: {
headers: requestHeaders,
},
});
// return null
}
export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};