Skip to content

Commit de48c27

Browse files
committed
hotfix: use correct URLs for redirects; don't parse JSON from JSON
1 parent 171405b commit de48c27

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

middleware.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async function customDomainMiddleware (request, domain, subName) {
4040
return redirectToAuthSync(searchParams, domain, signup)
4141
}
4242
// if we have a verification token, exchange it for a session token
43-
if (searchParams.has('token')) return establishAuthSync(searchParams)
43+
if (searchParams.has('token')) return establishAuthSync(request, searchParams)
4444

4545
// Territory URLs
4646
// if sub param exists and doesn't match the domain's subname, update it
@@ -84,20 +84,25 @@ async function redirectToAuthSync (searchParams, domain, signup) {
8484

8585
// if we have a callbackUrl, we need to set it as redirectUri
8686
if (searchParams.has('callbackUrl')) {
87-
syncUrl.searchParams.set('redirectUri', searchParams.get('callbackUrl'))
87+
const callbackUrl = searchParams.get('callbackUrl')
88+
// extract just the path portion if it's a full URL
89+
const redirectUri = callbackUrl.startsWith('http')
90+
? new URL(callbackUrl).pathname
91+
: callbackUrl
92+
syncUrl.searchParams.set('redirectUri', redirectUri)
8893
}
8994

9095
return NextResponse.redirect(syncUrl)
9196
}
9297

9398
// POST to /api/auth/sync and set the session cookie
94-
async function establishAuthSync (searchParams) {
99+
async function establishAuthSync (request, searchParams) {
95100
// get the verification token from the search params
96101
const token = searchParams.get('token')
97102
// get the redirectUri from the search params
98103
const redirectUri = searchParams.get('redirectUri') || '/'
99104
// prepare redirect to the redirectUri
100-
const res = NextResponse.redirect(decodeURIComponent(redirectUri))
105+
const res = NextResponse.redirect(new URL(decodeURIComponent(redirectUri), request.url))
101106

102107
// POST to /api/auth/sync to exchange verification token for session token
103108
const response = await fetch(`${SN_MAIN_DOMAIN.origin}/api/auth/sync`, {
@@ -114,7 +119,7 @@ async function establishAuthSync (searchParams) {
114119
const data = await response.json()
115120
if (data.status === 'ERROR') {
116121
// if the response is an error, redirect to the home page
117-
return NextResponse.redirect('/')
122+
return NextResponse.redirect(new URL('/', request.url))
118123
}
119124

120125
// set the session cookie

pages/api/auth/sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default async function handler (req, res) {
1313
// exchange a verification token for an ephemeral session token
1414
if (req.method === 'POST') {
1515
// a verification token is received from the middleware
16-
const { verificationToken } = JSON.parse(req.body)
16+
const { verificationToken } = req.body
1717
if (!verificationToken) {
1818
return res.status(400).json({ status: 'ERROR', reason: 'verification token is required' })
1919
}

0 commit comments

Comments
 (0)