-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
48 lines (39 loc) · 1.28 KB
/
middleware.ts
File metadata and controls
48 lines (39 loc) · 1.28 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
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
const protectedPaths = ['/dashboard', '/compose', '/history']
const isProtectedPath = protectedPaths.some((path) =>
req.nextUrl.pathname.startsWith(path)
)
if (isProtectedPath && !session) {
return NextResponse.redirect(new URL('/login', req.url))
}
// Redirect authenticated users away from auth pages
const authPaths = ['/login']
const isAuthPath = authPaths.some((path) =>
req.nextUrl.pathname.startsWith(path)
)
if (isAuthPath && session) {
return NextResponse.redirect(new URL('/dashboard', req.url))
}
return res
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - api routes (handled separately)
*/
'/((?!_next/static|_next/image|favicon.ico|api).*)',
],
}