forked from batuhanbilginn/makr-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
30 lines (23 loc) · 843 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
import { createMiddlewareSupabaseClient } from "@supabase/auth-helpers-nextjs";
import { NextResponse } from "next/server";
import type { Database } from "@/types/supabase";
import type { NextRequest } from "next/server";
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
const pathname = req.nextUrl.pathname;
const supabase = createMiddlewareSupabaseClient<Database>({ req, res });
const {
data: { session },
} = await supabase.auth.getSession();
if (!session && pathname.startsWith("/chat")) {
const url = new URL(req.url);
url.pathname = "/login";
return NextResponse.redirect(url);
}
if (session && (pathname === "/" || pathname === "/#")) {
const url = new URL(req.url);
url.pathname = "/chat";
return NextResponse.redirect(url);
}
return res;
}