-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
33 lines (27 loc) · 1 KB
/
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
31
32
33
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()
// Protect routes that require authentication
const protectedPaths = ['/settings']
if (!session && protectedPaths.some(path => req.nextUrl.pathname.startsWith(path))) {
const redirectUrl = req.nextUrl.clone()
redirectUrl.pathname = '/auth'
return NextResponse.redirect(redirectUrl)
}
// Redirect to settings if already logged in and trying to access auth page
if (session && req.nextUrl.pathname === '/auth') {
const redirectUrl = req.nextUrl.clone()
redirectUrl.pathname = '/settings'
return NextResponse.redirect(redirectUrl)
}
return res
}
export const config = {
matcher: ['/settings', '/auth'],
}