-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
43 lines (33 loc) · 1016 Bytes
/
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 { auth } from "@/auth";
import { NextRequest, NextResponse } from "next/server";
import {
API_AUTH_PREFIX,
AUTH_ROUTES,
DEFAULT_LOGIN_REDIRECT,
PUBLIC_ROUTES,
} from "./routes";
import { BASE_URL } from "./config/config";
export async function middleware(req: NextRequest) {
const { nextUrl } = req;
const session = await auth();
const isLoggedIn: Boolean = session === null ? false : true;
const isApiAuthRoute = API_AUTH_PREFIX.includes(nextUrl.pathname);
const isPublicRoute = PUBLIC_ROUTES.includes(nextUrl.pathname);
const isAuthRoute = AUTH_ROUTES.includes(nextUrl.pathname);
if (isApiAuthRoute) {
return null;
}
if (isAuthRoute) {
if (isLoggedIn) {
return NextResponse.redirect(`${BASE_URL}${DEFAULT_LOGIN_REDIRECT}`);
}
return null;
}
if (!isLoggedIn && !isPublicRoute) {
return NextResponse.redirect(`${BASE_URL}/signin`);
}
return null;
}
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};