-
Notifications
You must be signed in to change notification settings - Fork 4
/
middleware.ts
69 lines (62 loc) · 2.04 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
import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import type { Database } from '@/types_db'
export const CORS_HEADERS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
}
const apiRoutes = [
'/api/whatsapp',
'/api/insights',
'/api/telegram',
]
export async function middleware(req: NextRequest) {
if (req.method === 'OPTIONS') {
return new Response(null, {
headers: CORS_HEADERS,
})
}
const res = NextResponse.next()
if (
apiRoutes.some((route) => req.nextUrl.pathname.includes(route))
) {
return res
}
const supabase = createMiddlewareClient<Database>({ req, res })
const { data: { session } } = await supabase.auth.getSession()
if (process.env.NEXT_RUNTIME === 'nodejs') {
/** Conditional import required for use with Next middleware to avoid a webpack error
* https://nextjs.org/docs/pages/building-your-application/routing/middleware */
try {
const { registerHighlight } = await import('@highlight-run/next/server')
registerHighlight({
projectID: process.env.NEXT_PUBLIC_HIGHLIGHT_PROJECT_ID!,
})
console.log('Highlight instrumentation registered')
} catch (error) {
console.error(error)
}
}
// ugly hack due to https://github.com/orgs/supabase/discussions/16135#discussioncomment-6642592
if (session) {
res.cookies.set('supabase.auth.token', session.access_token, {
path: '/',
maxAge: 60 * 60 * 24 * 30,
sameSite: 'lax',
secure: process.env.NODE_ENV === 'production',
})
res.cookies.set('supabase.auth.refresh_token', session.refresh_token, {
path: '/',
maxAge: 60 * 60 * 24 * 30,
sameSite: 'lax',
secure: process.
env.NODE_ENV === 'production',
})
} else if (req.cookies.get('supabase.auth.token')) {
await supabase.auth.refreshSession({
refresh_token: req.cookies.get('supabase.auth.refresh_token')!.value,
})
}
return res
}