-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathroute.ts
More file actions
123 lines (110 loc) · 3.15 KB
/
Copy pathroute.ts
File metadata and controls
123 lines (110 loc) · 3.15 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
import { redirect } from 'next/navigation'
import { AUTH_URLS, PROTECTED_URLS } from '@/configs/urls'
import { l, serializeErrorForLog } from '@/core/shared/clients/logger/logger'
import { createClient } from '@/core/shared/clients/supabase/server'
import { encodedRedirect } from '@/lib/utils/auth'
export async function GET(request: Request) {
// The `/auth/callback` route is required for the server-side auth flow implemented
// by the SSR package. It exchanges an auth code for the user's session.
// https://supabase.com/docs/guides/auth/server-side/nextjs
const requestUrl = new URL(request.url)
const code = requestUrl.searchParams.get('code')
const origin = requestUrl.origin
const returnTo = requestUrl.searchParams.get('returnTo')?.toString()
const redirectTo = requestUrl.searchParams.get('redirect_to')?.toString()
l.info(
{
key: 'auth_callback:request',
context: {
code: !!code,
origin,
returnTo,
redirectTo,
},
},
`Auth callback request received`
)
if (code) {
const supabase = await createClient()
const { data, error } = await supabase.auth.exchangeCodeForSession(code)
if (error) {
l.error(
{
key: 'auth_callback:supabase_error',
error: serializeErrorForLog(error),
context: {
hasCode: !!code,
origin,
returnTo,
redirectTo,
},
},
`Auth callback supabase error: ${error.message}`
)
throw encodedRedirect('error', AUTH_URLS.SIGN_IN, error.message)
} else {
const userId = data.user?.id
if (!data.session?.access_token || !userId) {
throw encodedRedirect(
'error',
AUTH_URLS.SIGN_IN,
'Missing session after auth callback'
)
}
l.info(
{
key: 'auth_callback:otp_exchanged',
user_id: userId,
},
`OTP successfully exchanged for user session`
)
}
}
if (redirectTo) {
const returnToUrl = new URL(redirectTo, origin)
if (returnToUrl.origin === origin) {
l.info(
{
key: 'auth_callback:redirecting_to',
context: {
redirectTo,
},
},
`Redirecting to ${redirectTo}`
)
return redirect(redirectTo)
}
}
// If returnTo is present, redirect there
if (returnTo) {
// Ensure returnTo is a relative URL to prevent open redirect vulnerabilities
const returnToUrl = new URL(returnTo, origin)
if (returnTo === PROTECTED_URLS.ACCOUNT_SETTINGS) {
returnToUrl.searchParams.set('reauth', '1')
return redirect(returnToUrl.toString())
}
if (returnToUrl.origin === origin) {
l.info(
{
key: 'auth_callback:returning_to',
context: {
returnTo,
},
},
`Returning to ${returnTo}`
)
return redirect(returnTo)
}
}
// Default redirect to dashboard
l.info(
{
key: 'auth_callback:redirecting_to_dashboard',
context: {
returnTo,
},
},
`Redirecting to dashboard`
)
return redirect(PROTECTED_URLS.DASHBOARD)
}