-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
52 lines (43 loc) · 1.86 KB
/
Copy pathmiddleware.ts
File metadata and controls
52 lines (43 loc) · 1.86 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
import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import type { Database } from '@/types/database'
// Routes that require authentication
const PROTECTED_ROUTES = ['/dashboard', '/tasks', '/profile', '/wallet', '/settings']
// Routes only for unauthenticated users (redirect to dashboard if logged in)
const AUTH_ROUTES = ['/auth/login', '/auth/signup', '/auth/forgot-password']
export async function middleware(request: NextRequest) {
const response = NextResponse.next()
const supabase = createMiddlewareClient<Database>({ req: request, res: response })
// IMPORTANT: always call getSession() in middleware — this refreshes the
// access token using the refresh token if it has expired, and sets the
// updated cookies on the response so Server Components see the valid session.
const {
data: { session },
} = await supabase.auth.getSession()
const { pathname } = request.nextUrl
// Redirect unauthenticated users away from protected routes
if (!session && PROTECTED_ROUTES.some((route) => pathname.startsWith(route))) {
const loginUrl = new URL('/auth/login', request.url)
loginUrl.searchParams.set('next', pathname)
return NextResponse.redirect(loginUrl)
}
// Redirect authenticated users away from auth pages
if (session && AUTH_ROUTES.some((route) => pathname.startsWith(route))) {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
return response
}
export const config = {
matcher: [
/*
* Match all request paths EXCEPT:
* - _next/static (static files)
* - _next/image (image optimization)
* - favicon.ico
* - public folder files
* - api routes (handled separately)
*/
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
}