-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
27 lines (25 loc) · 890 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
import { NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
export { default } from "next-auth/middleware";
// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
const token = await getToken({ req: request });
const url = request.nextUrl;
if (
token &&
(url.pathname.startsWith("/sign-in") ||
url.pathname.startsWith("/sign-up") ||
url.pathname.startsWith("/verify") ||
url.pathname === '/')
) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
if (!token && url.pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/", request.url));
}
return NextResponse.next();
}
export const config = {
//where will be active the middleware
matcher: ["/sign-in", "/sign-up", "/dashboard/:path*"],
};