-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
27 lines (22 loc) · 807 Bytes
/
middleware.ts
File metadata and controls
27 lines (22 loc) · 807 Bytes
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
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()
// If there's no session and the user is trying to access a protected route
if (!session && (
req.nextUrl.pathname.startsWith('/dashboard') ||
req.nextUrl.pathname.startsWith('/tasks') ||
req.nextUrl.pathname.startsWith('/notes')
)) {
return NextResponse.redirect(new URL('/login', req.url))
}
return res
}
export const config = {
matcher: ['/dashboard/:path*', '/tasks/:path*', '/notes/:path*'],
}