-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
28 lines (23 loc) · 864 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
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();
// Protected routes
if (req.nextUrl.pathname.startsWith('/dashboard') ||
req.nextUrl.pathname.startsWith('/properties') ||
req.nextUrl.pathname.startsWith('/maintenance')) {
if (!session) {
return NextResponse.redirect(new URL('/auth/login', req.url));
}
}
// Auth routes (when already logged in)
if (req.nextUrl.pathname.startsWith('/auth') && session) {
return NextResponse.redirect(new URL('/dashboard', req.url));
}
return res;
}