-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
31 lines (24 loc) · 899 Bytes
/
middleware.ts
File metadata and controls
31 lines (24 loc) · 899 Bytes
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
import { createMiddlewareClient } from "@supabase/auth-helpers-nextjs";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
const supabase = createMiddlewareClient({ req, res });
const {
data: { session },
} = await supabase.auth.getSession();
const { pathname } = req.nextUrl;
// Redirect logged-in users away from auth pages
if (session && (pathname === "/login" || pathname === "/signup")) {
return NextResponse.redirect(new URL("/dashboard", req.url));
}
// Redirect guests away from protected pages
if (!session && pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/login", req.url));
}
return res;
}
// 👇 critical!
export const config = {
matcher: ["/login", "/signup", "/dashboard/:path*"],
};