-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.tsx
29 lines (25 loc) · 1008 Bytes
/
middleware.tsx
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
import { NextResponse, type NextRequest } from "next/server";
import { createSSRClient } from "./app/config/server";
export async function middleware(request: NextRequest) {
const supabase = createSSRClient();
const { data, error } = await supabase.auth.getSession();
if (error) throw error;
if (
!data.session &&
!request.nextUrl.pathname.startsWith("/dashboard/auth/login") &&
!request.nextUrl.pathname.startsWith("/dashboard/auth/register") &&
!request.nextUrl.pathname.startsWith("/dashboard/auth/success")
) {
return NextResponse.redirect(new URL("/dashboard/auth/login", request.url));
} else if (
data.session &&
request.nextUrl.pathname.startsWith("/dashboard/auth/login") &&
request.nextUrl.pathname.startsWith("/dashboard/auth/register") &&
request.nextUrl.pathname.startsWith("/dashboard/auth/success")
) {
return NextResponse.redirect(new URL("/dashboard/", request.url));
}
}
export const config = {
matcher: "/dashboard/:path*",
};