-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
39 lines (32 loc) · 1.03 KB
/
Copy pathmiddleware.ts
File metadata and controls
39 lines (32 loc) · 1.03 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
import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
import { createServerClient } from "@/lib/supabase-server"
export async function middleware(request: NextRequest) {
if (request.nextUrl.pathname.startsWith("/dashboard")) {
try {
const requestHeaders = new Headers(request.headers)
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
})
const supabase = createServerClient()
const {
data: { session },
} = await supabase.auth.getSession()
if (!session) {
const signInUrl = new URL("/sign-in", request.url)
signInUrl.searchParams.set("redirect", request.nextUrl.pathname)
return NextResponse.redirect(signInUrl)
}
return response
} catch (error) {
console.error("Middleware error:", error)
return NextResponse.redirect(new URL("/sign-in", request.url))
}
}
return NextResponse.next()
}
export const config = {
matcher: ["/dashboard/:path*"],
}