-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
44 lines (37 loc) · 1.01 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
import NextAuth from "next-auth";
import authConfig from "@/auth.config";
import {
API_AUTH_PREFIX,
DEFAULT_REDIRECT_PATH_AFTER_SIGN_IN,
DEFAULT_REDIRECT_PATH_BEFORE_SIGN_IN,
authRoutes,
publicRoutes,
} from "@/routes";
const { auth: middleware } = NextAuth(authConfig);
export default middleware((req) => {
const { nextUrl } = req;
const isAuth = !!req.auth;
const isApiRoute = nextUrl.pathname.startsWith(API_AUTH_PREFIX);
const isAuthRoute = authRoutes.includes(nextUrl.pathname);
const isPublicRoute = publicRoutes.includes(nextUrl.pathname);
if (isApiRoute) {
return null;
}
if (isAuthRoute) {
if (isAuth) {
return Response.redirect(
new URL(DEFAULT_REDIRECT_PATH_AFTER_SIGN_IN, nextUrl)
);
}
return null;
}
if (!isAuth && !isPublicRoute) {
return Response.redirect(
new URL(DEFAULT_REDIRECT_PATH_BEFORE_SIGN_IN, nextUrl)
);
}
return null;
});
export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};