forked from vercel/ai-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
120 lines (112 loc) · 3.69 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
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,
},
})
// 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: 'true', 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()
// 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' },
],
},
]
}