-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
51 lines (42 loc) · 1.46 KB
/
Copy pathproxy.ts
File metadata and controls
51 lines (42 loc) · 1.46 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
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
import { NextResponse } from 'next/server'
const isProtectedRoute = createRouteMatcher([
'/app(.*)',
'/gestoria(.*)',
'/onboarding(.*)',
'/api/documents(.*)',
'/api/chat(.*)',
'/api/export(.*)',
'/api/settings(.*)',
'/api/stripe/(.*)',
'/api/gestoria(.*)',
'/api/whitelabel(.*)',
'/api/stats(.*)',
])
const isPublicRoute = createRouteMatcher([
'/api/webhooks/(.*)',
'/invite/(.*)',
])
const isAuthRoute = createRouteMatcher(['/sign-in(.*)', '/sign-up(.*)'])
export const proxy = clerkMiddleware(async (auth, req) => {
// Webhooks son públicos — Inngest y Clerk verifican con sus propias firmas
if (isPublicRoute(req)) return
const { userId } = await auth()
// Si está autenticado y va a / o a sign-in/sign-up → redirige a la app
if (userId && (req.nextUrl.pathname === '/' || isAuthRoute(req))) {
return NextResponse.redirect(new URL('/app/dashboard', req.url))
}
// Rutas protegidas requieren autenticación
if (isProtectedRoute(req)) await auth.protect()
// Forward hostname for white-label resolution in server components
const hostname = req.headers.get('host') ?? ''
const res = NextResponse.next()
res.headers.set('X-Hostname', hostname)
return res
})
export const config = {
matcher: [
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
'/(api|trpc)(.*)',
],
}