-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
67 lines (59 loc) · 2.06 KB
/
Copy pathproxy.ts
File metadata and controls
67 lines (59 loc) · 2.06 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
import { createServerClient } from '@supabase/ssr'
import { NextResponse, type NextRequest } from 'next/server'
import { getPublicOrigin } from '@/lib/request-origin'
import {
getSupabasePublicAnonKey,
getSupabasePublicUrl,
hasSupabasePublicConfig,
} from '@/lib/supabase-public-env'
export async function proxy(request: NextRequest) {
if (!hasSupabasePublicConfig()) {
if (process.env.NODE_ENV === 'development') {
console.warn(
'[proxy] NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY are missing — auth gate skipped for local dev. Add them to .env.local (see README.md).'
)
return NextResponse.next({ request })
}
throw new Error(
'NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY are required. See https://supabase.com/dashboard/project/_/settings/api'
)
}
let supabaseResponse = NextResponse.next({ request })
const supabase = createServerClient(
getSupabasePublicUrl(),
getSupabasePublicAnonKey(),
{
cookies: {
getAll() {
return request.cookies.getAll()
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value }) => request.cookies.set(name, value))
supabaseResponse = NextResponse.next({ request })
cookiesToSet.forEach(({ name, value, options }) =>
supabaseResponse.cookies.set(name, value, options)
)
},
},
}
)
const {
data: { user },
} = await supabase.auth.getUser()
if (!user) {
const login = new URL('/login', getPublicOrigin(request))
login.searchParams.set('redirect', request.nextUrl.pathname + request.nextUrl.search)
return NextResponse.redirect(login)
}
if (!user.email_confirmed_at) {
const path = request.nextUrl.pathname
if (path.startsWith('/verify-email')) {
return supabaseResponse
}
return NextResponse.redirect(new URL('/verify-email', getPublicOrigin(request)))
}
return supabaseResponse
}
export const config = {
matcher: ['/feed/:path*', '/onboarding/:path*', '/settings/:path*', '/verify-email'],
}