-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
72 lines (59 loc) · 2.22 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
import { cookies } from 'next/headers'
import { NextRequest, NextResponse } from 'next/server'
import { decrypt } from './lib/session'
import { updateSession } from './lib/supabase/middleware'
// const isProtectedRoute = createRouteMatcher([
// '/web-builder/sign-in(.*)',
// '/web-builder/sign-up(.*)'
// ])
// const isProtectedRoute = createRouteMatcher(['/web-builder/project(.*)'])
// export default clerkMiddleware((auth, request) => {
// // if (!isProtectedRoute(request)) {
// // auth().protect()
// // }
// if (!auth().userId && isProtectedRoute(request)) {
// return auth().redirectToSignIn()
// }
// })
// export default clerkMiddleware()
export async function middleware(request: NextRequest) {
// update user's auth session (supabase)
// await updateSession(request)
const protectedRoutes = '/web-builder/project'
const currentPath = request.nextUrl.pathname
const isProtectedRoute = currentPath.includes(protectedRoutes)
if (isProtectedRoute) {
const cookie = (await cookies()).get('session')?.value
// if (typeof cookie === 'undefined') {
// return NextResponse.redirect(
// new URL(`/web-builder/sign-in/${request.nextUrl.pathname}`, request.nextUrl)
// )
// }
if (typeof cookie !== 'undefined') {
const session = await decrypt(cookie)
if (!session?.userId) {
return NextResponse.redirect(new URL('/web-builder/sign-in', request.nextUrl))
}
}
}
return NextResponse.next()
}
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)
* Feel free to modify this pattern to include more paths.
*/
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)'
]
// matcher: [
// // Skip Next.js internals and all static files, unless found in search params
// '/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
// // Always run for API routes
// '/(api|trpc)(.*)'
// ]
}