-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
40 lines (33 loc) · 1.02 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
// import { useSession } from "next-auth/react";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
// const session = useSession();
const pathname = request.nextUrl.pathname;
const isPublicPath =
pathname == "/" ||
pathname == "/verifyemail" ||
pathname == "/forgotpassword";
const token = request.cookies.get("token");
const nextAuthToken =
request.cookies.get("next-auth.session-token") ||
request.cookies.get("__Secure-next-auth.session-token");
if (isPublicPath && token) {
return NextResponse.redirect(new URL("/home", request.url));
}
if (isPublicPath && nextAuthToken) {
return NextResponse.redirect(new URL("/home", request.url));
}
if (!isPublicPath && !token && !nextAuthToken) {
return NextResponse.redirect(new URL("/", request.url));
}
}
export const config = {
matcher: [
"/",
"/me",
"/verifyemail",
"/forgotpassword",
"/(dashboard)/[storeId]",
],
};