-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.complex.ts
More file actions
377 lines (317 loc) · 13.6 KB
/
middleware.complex.ts
File metadata and controls
377 lines (317 loc) · 13.6 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { createMiddlewareClient } from './lib/supabase'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// Supported locales - NEXT.JS 15 COMPATIBLE
export const locales = ['pt', 'en'] as const
export const defaultLocale = 'pt' as const
export type Locale = typeof locales[number]
// Admin configuration
const ADMIN_EMAIL = 'armazemsaojoaquimoficial@gmail.com'
const SESSION_TIMEOUT = 8 * 60 * 60 * 1000 // 8 hours in milliseconds
const ADMIN_CACHE_TTL = 5 * 60 * 1000 // 5 minutes cache TTL
// Edge Runtime compatible cache - memory optimized
interface AdminCacheEntry {
isAdmin: boolean
timestamp: number
sessionStart?: number
}
const adminCache = new Map<string, AdminCacheEntry>()
// Get locale from pathname - OPTIMIZED
function getLocale(pathname: string): Locale | null {
const segments = pathname.split('/')
const firstSegment = segments[1] as Locale
return locales.includes(firstSegment) ? firstSegment : null
}
// Check if path needs locale redirect - PERFORMANCE OPTIMIZED
function needsLocaleRedirect(pathname: string): boolean {
// Skip API routes, static files, and certain paths
if (
pathname.startsWith('/api/') ||
pathname.startsWith('/_next/') ||
pathname.startsWith('/favicon') ||
pathname.startsWith('/manifest') ||
pathname.startsWith('/robots') ||
pathname.startsWith('/sitemap') ||
pathname.includes('.') // Any file with extension
) {
return false
}
// Check if pathname already has a locale
return !locales.some(locale =>
pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
)
}
// Check if session has timed out
function isSessionExpired(sessionStart: number): boolean {
return Date.now() - sessionStart > SESSION_TIMEOUT
}
// Fast admin check with caching and session timeout
function getAdminFromCache(userId: string, email: string): { isAdmin: boolean; expired: boolean } | null {
// Always allow main admin email with fresh verification
if (email === ADMIN_EMAIL) {
return { isAdmin: true, expired: false }
}
const cacheKey = userId || email
const cached = adminCache.get(cacheKey)
if (!cached) {
return null // Cache miss
}
const now = Date.now()
const cacheExpired = (now - cached.timestamp) > ADMIN_CACHE_TTL
const sessionExpired = cached.sessionStart ? isSessionExpired(cached.sessionStart) : false
if (cacheExpired || sessionExpired) {
adminCache.delete(cacheKey) // Clean expired cache
return null
}
return { isAdmin: cached.isAdmin, expired: false }
}
// Update admin cache with session tracking
function setAdminCache(userId: string, email: string, isAdmin: boolean, sessionStart?: number): void {
const cacheKey = userId || email
adminCache.set(cacheKey, {
isAdmin,
timestamp: Date.now(),
sessionStart: sessionStart || Date.now()
})
}
// Clean expired cache entries periodically
function cleanExpiredCache(): void {
const now = Date.now()
for (const [key, entry] of adminCache.entries()) {
const cacheExpired = (now - entry.timestamp) > ADMIN_CACHE_TTL
const sessionExpired = entry.sessionStart ? isSessionExpired(entry.sessionStart) : false
if (cacheExpired || sessionExpired) {
adminCache.delete(key)
}
}
}
// Simplified admin authentication middleware
async function verifyAdminAccess(
supabase: any,
session: any,
userId: string,
email: string
): Promise<{ isAdmin: boolean; reason: string }> {
try {
// Primary check: Admin email
if (email === ADMIN_EMAIL) {
console.log('✅ MIDDLEWARE: Admin verified by email')
setAdminCache(userId, email, true, session.created_at ? new Date(session.created_at).getTime() : Date.now())
return { isAdmin: true, reason: 'admin_email' }
}
// Check cache first for performance
const cached = getAdminFromCache(userId, email)
if (cached !== null && !cached.expired) {
console.log('📋 MIDDLEWARE: Using cached admin result')
return { isAdmin: cached.isAdmin, reason: 'cache' }
}
// Database verification as fallback
try {
const { data: profile, error } = await supabase
.from('profiles')
.select('role')
.eq('id', userId)
.single()
if (!error && profile) {
const isAdmin = profile.role === 'admin'
setAdminCache(userId, email, isAdmin, session.created_at ? new Date(session.created_at).getTime() : Date.now())
console.log(`✅ MIDDLEWARE: Database verification - isAdmin: ${isAdmin}`)
return { isAdmin, reason: 'database' }
}
} catch (dbError) {
console.warn('⚠️ MIDDLEWARE: Database check failed:', dbError)
}
// Not admin
console.log('❌ MIDDLEWARE: User is not admin')
setAdminCache(userId, email, false, session.created_at ? new Date(session.created_at).getTime() : Date.now())
return { isAdmin: false, reason: 'not_admin' }
} catch (error: any) {
console.error('❌ MIDDLEWARE: Admin verification error:', error)
// Fallback to email check for critical errors
if (email === ADMIN_EMAIL) {
console.log('⚠️ MIDDLEWARE: Error occurred but admin email - allowing access')
setAdminCache(userId, email, true, session.created_at ? new Date(session.created_at).getTime() : Date.now())
return { isAdmin: true, reason: 'admin_email_fallback' }
}
return { isAdmin: false, reason: 'verification_error' }
}
}
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// FAST EARLY RETURNS for performance
// Skip static files and non-admin API routes
if (
pathname.startsWith('/_next/') ||
pathname.startsWith('/favicon') ||
pathname.startsWith('/manifest') ||
pathname.startsWith('/robots') ||
pathname.startsWith('/sitemap') ||
pathname.includes('.')
) {
return NextResponse.next()
}
// Handle locale redirects (skip for API routes)
if (!pathname.startsWith('/api/') && needsLocaleRedirect(pathname)) {
// Redirect root to default locale
if (pathname === '/') {
return NextResponse.redirect(new URL(`/${defaultLocale}`, request.url))
}
// Redirect other paths to include default locale
return NextResponse.redirect(new URL(`/${defaultLocale}${pathname}`, request.url))
}
// Extract locale from pathname for further processing
const locale = getLocale(pathname)
const pathWithoutLocale = pathname.replace(`/${locale}`, '') || '/'
// Periodic cache cleanup (1% chance per request)
if (Math.random() < 0.01) {
cleanExpiredCache()
}
try {
// Create response that will be modified with headers/cookies
const response = NextResponse.next()
// Add security headers for all requests
response.headers.set('X-DNS-Prefetch-Control', 'on')
response.headers.set('X-Frame-Options', 'DENY')
response.headers.set('X-Content-Type-Options', 'nosniff')
response.headers.set('X-XSS-Protection', '1; mode=block')
response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin')
// Create Supabase client for auth handling
const supabase = createMiddlewareClient(request, response)
// Get session with error handling for Edge Runtime
let session = null
try {
// First try to get session from cookies
const { data: { session: userSession }, error } = await supabase.auth.getSession()
session = userSession
// If no session from cookies, try Authorization header
if (!session && request.headers.get('authorization')) {
const authHeader = request.headers.get('authorization')
if (authHeader?.startsWith('Bearer ')) {
const token = authHeader.split(' ')[1]
console.log('🔑 MIDDLEWARE: Trying Authorization header...')
try {
// Create a new client with the token
const { createClient } = await import('@supabase/supabase-js')
const tokenClient = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
)
const { data: { user }, error: tokenError } = await tokenClient.auth.getUser(token)
if (!tokenError && user) {
console.log('✅ MIDDLEWARE: Session found via Authorization header')
session = {
user,
access_token: token,
expires_at: Math.floor(Date.now() / 1000) + 3600 // 1 hour from now
}
}
} catch (tokenError) {
console.warn('⚠️ MIDDLEWARE: Authorization header error:', tokenError)
}
}
}
// Refresh user data if session exists (Next.js 15 compatibility)
if (session && !error) {
await supabase.auth.getUser()
}
} catch (authError) {
console.warn('⚠️ MIDDLEWARE: Auth error:', authError)
}
// Admin routes protection (both page routes and API routes)
if (pathWithoutLocale.startsWith('/admin') || pathname.startsWith('/api/admin/')) {
console.log('🔒 MIDDLEWARE: Admin route accessed:', pathname)
// No session - redirect to auth or return 401 for API routes
if (!session?.user) {
console.log('❌ MIDDLEWARE: No session for admin route')
// For API routes, return 401 instead of redirect
if (pathname.startsWith('/api/admin/')) {
return NextResponse.json(
{ error: 'Authentication required', code: 'NO_SESSION' },
{ status: 401 }
)
}
// For page routes, redirect to auth
const redirectUrl = request.nextUrl.clone()
redirectUrl.pathname = `/${locale}/auth`
redirectUrl.searchParams.set('message', 'É necessário fazer login para acessar o painel administrativo')
redirectUrl.searchParams.set('redirect', pathname)
return NextResponse.redirect(redirectUrl)
}
const userId = session.user.id
const email = session.user.email || ''
console.log('👤 MIDDLEWARE: Checking admin access for:', email)
// Verify admin access
const { isAdmin, reason } = await verifyAdminAccess(supabase, session, userId, email)
if (!isAdmin) {
console.log('❌ MIDDLEWARE: Admin access denied:', reason)
// For API routes, return 403 instead of redirect
if (pathname.startsWith('/api/admin/')) {
return NextResponse.json(
{
error: 'Access denied - admin only',
code: 'ACCESS_DENIED',
reason
},
{ status: 403 }
)
}
// For page routes, redirect to unauthorized
const redirectUrl = request.nextUrl.clone()
redirectUrl.pathname = `/${locale}/unauthorized`
// Set appropriate message based on reason
let message = 'Acesso negado - apenas administradores podem acessar esta área'
if (reason === 'session_expired') {
message = 'Sua sessão expirou. Faça login novamente.'
redirectUrl.pathname = `/${locale}/auth`
redirectUrl.searchParams.set('redirect', pathname)
}
redirectUrl.searchParams.set('message', message)
return NextResponse.redirect(redirectUrl)
}
console.log('✅ MIDDLEWARE: Admin access granted:', reason)
// Add admin session headers for additional security
response.headers.set('X-Admin-Session', 'true')
response.headers.set('X-Admin-Verified', reason)
}
return response
} catch (error: any) {
console.error('❌ MIDDLEWARE: Critical error:', error)
// If there's a critical error and it's an admin route, redirect to auth or return error
if (pathWithoutLocale.startsWith('/admin') || pathname.startsWith('/api/admin/')) {
// For API routes, return 500
if (pathname.startsWith('/api/admin/')) {
return NextResponse.json(
{ error: 'Internal server error', code: 'MIDDLEWARE_ERROR' },
{ status: 500 }
)
}
// For page routes, redirect to auth
const redirectUrl = request.nextUrl.clone()
redirectUrl.pathname = `/${locale || defaultLocale}/auth`
redirectUrl.searchParams.set('error', 'middleware_error')
redirectUrl.searchParams.set('message', 'Erro na verificação de autenticação. Tente novamente.')
return NextResponse.redirect(redirectUrl)
}
// For non-admin routes, continue with basic security headers
const response = NextResponse.next()
response.headers.set('X-Frame-Options', 'DENY')
response.headers.set('X-Content-Type-Options', 'nosniff')
return response
}
}
// NEXT.JS 15 OPTIMIZED MATCHER - Performance Enhanced
export const config = {
matcher: [
/*
* Match all request paths except:
* - Static files (_next/static, _next/image)
* - Public files (favicon, manifest, robots, sitemap)
* - File extensions (.png, .jpg, .css, .js, etc.)
* - Webpack HMR
*
* INCLUDES: /api/admin/* routes for admin protection
* PERFORMANCE: More specific patterns for better Edge Runtime performance
*/
'/((?!_next/static|_next/image|_next/webpack-hmr|favicon|manifest|robots|sitemap|.*\\..*).*)',
],
}