-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
63 lines (51 loc) · 1.81 KB
/
Copy pathmiddleware.ts
File metadata and controls
63 lines (51 loc) · 1.81 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { type NextRequest, NextResponse } from "next/server";
import { createClient } from "@/lib/supabase/middleware";
/**
* Protected routes that require authentication
*/
const protectedRoutes = ["/dashboard", "/meeting", "/settings", "/profile"];
/**
* Auth routes - redirect to dashboard if already logged in
*/
const authRoutes = ["/login"];
/**
* Public routes - accessible to everyone
*/
const publicRoutes = ["/", "/auth/callback", "/terms", "/privacy"];
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Create Supabase client and get user
const { user, response } = await createClient(request);
// Check if the route is protected
const isProtectedRoute = protectedRoutes.some(
(route) => pathname === route || pathname.startsWith(`${route}/`)
);
// Check if the route is an auth route
const isAuthRoute = authRoutes.some(
(route) => pathname === route || pathname.startsWith(`${route}/`)
);
// If user is not authenticated and trying to access protected route
if (isProtectedRoute && !user) {
const redirectUrl = new URL("/login", request.url);
redirectUrl.searchParams.set("redirect", pathname);
return NextResponse.redirect(redirectUrl);
}
// If user is authenticated and trying to access auth route (login)
if (isAuthRoute && user) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
return response;
}
export const config = {
matcher: [
/*
* Match all request paths except for:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder files (images, etc.)
* - API routes (handled separately)
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};