forked from supabase-community/vercel-ai-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
156 lines (144 loc) · 4.8 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
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
import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse, NextRequest } from 'next/server'
import { createServerClient, type CookieOptions } from '@supabase/ssr'
import { cookies } from 'next/headers'
export async function middleware(request: NextRequest) {
// const res = NextResponse.next()
let response = NextResponse.next({
request: {
headers: request.headers,
},
})
// Added CSP code
// directly from https://nextjs.org/docs/app/building-your-application/configuring/content-security-policy
const nonce = Buffer.from(crypto.randomUUID()).toString('base64')
const cspHeader = process.env.DEBUG_MODE ? `` : `
default-src 'self' 'unsafe-inline';
script-src 'self' 'nonce-${nonce}' 'strict-dynamic' 'unsafe-inline';
style-src 'self' 'nonce-${nonce}' 'unsafe-inline';
img-src 'self' blob: data:;
font-src 'self';
object-src 'none';
base-uri 'self';
form-action 'self';
frame-ancestors 'none';
block-all-mixed-content;
upgrade-insecure-requests;
`
// // Replace newline characters and spaces
const contentSecurityPolicyHeaderValue = cspHeader
.replace(/\s{2,}/g, ' ')
.trim()
const requestHeaders = new Headers(request.headers)
requestHeaders.set('x-nonce', nonce)
requestHeaders.set(
'Content-Security-Policy',
contentSecurityPolicyHeaderValue
)
response.headers.set(
'Content-Security-Policy',
contentSecurityPolicyHeaderValue
)
// Create a Supabase client configured to use cookies
// const supabase = createMiddlewareClient({ req, res })
// Refresh session if expired - required for Server Components
// https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-session-with-middleware
// const {
// data: { session }
// } = await supabase.auth.getSession()
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookieOptions: {domain: '', secure: 'false', maxAge: 604800, path: '', sameSite: 'None'},
cookies: {
get(name: string) {
return request.cookies.get(name)?.value
},
set(name: string, value: string, options: CookieOptions) {
request.cookies.set({
name,
value,
...options,
})
response = NextResponse.next({
request: {
headers: request.headers,
},
})
response.cookies.set({
name,
value,
...options,
})
},
remove(name: string, options: CookieOptions) {
request.cookies.set({
name,
value: '',
...options,
})
response = NextResponse.next({
request: {
headers: request.headers,
},
})
response.cookies.set({
name,
value: '',
...options,
})
},
},
}
)
// console.log(await supabase.auth.getUser())
let {data: session} = await supabase.auth.getUser()
await supabase.auth.getSession()
const cookieStore = cookies()
// OPTIONAL: this forces users to be logged in to use the chatbot.
// If you want to allow anonymous users, simply remove the check below.
const user_id = request.nextUrl.searchParams.get('user_id')
if (request.url.includes('/api/healthcheck')){
return response
}
if (
!session.user &&
!request.url.includes('/sign-in') &&
!request.url.includes('/sign-up') &&
!request.url.includes('/reset-password')
) {
const redirectUrl = request.nextUrl.clone()
redirectUrl.pathname = '/sign-in'
redirectUrl.searchParams.set(`redirectedFrom`, request.nextUrl.pathname)
user_id ? redirectUrl.searchParams.set(`user_id`, user_id) : undefined
return NextResponse.redirect(redirectUrl)
}
else if (session.user && request.url.includes('/reset-password') && request.url.includes('/sign-up') && request.url.includes('/sign-in')){
const redirectUrl = request.nextUrl.clone()
redirectUrl.pathname = '/'
user_id ? redirectUrl.searchParams.set(`user_id`, user_id) : undefined
return NextResponse.redirect(redirectUrl)
}
return response
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - share (publicly shared chats)
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
// '/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
{
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
]
}