Skip to content

Commit 872691b

Browse files
committed
auth sync: responses with custom domain subname headers; redirect to error page; inject custom domain header
1 parent 4603878 commit 872691b

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

middleware.js

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ async function customDomainMiddleware (request, domain, subName) {
3737
// if the user is trying to login or signup, redirect to the Auth Sync API
3838
if (pathname.startsWith('/login') || pathname.startsWith('/signup')) {
3939
const signup = pathname.startsWith('/signup')
40-
return redirectToAuthSync(searchParams, domain, signup)
40+
return redirectToAuthSync(searchParams, domain, signup, headers)
4141
}
4242
// if we have a verification token, exchange it for a session token
43-
if (searchParams.has('token')) return establishAuthSync(request, searchParams)
43+
if (searchParams.has('token')) return establishAuthSync(request, searchParams, headers)
4444

4545
// Territory URLs
4646
// if sub param exists and doesn't match the domain's subname, update it
@@ -73,7 +73,7 @@ async function customDomainMiddleware (request, domain, subName) {
7373
}
7474

7575
// redirect to the Auth Sync API
76-
async function redirectToAuthSync (searchParams, domain, signup) {
76+
async function redirectToAuthSync (searchParams, domain, signup, headers) {
7777
const syncUrl = new URL('/api/auth/sync', SN_MAIN_DOMAIN)
7878
syncUrl.searchParams.set('domain', domain)
7979

@@ -92,39 +92,48 @@ async function redirectToAuthSync (searchParams, domain, signup) {
9292
syncUrl.searchParams.set('redirectUri', redirectUri)
9393
}
9494

95-
return NextResponse.redirect(syncUrl)
95+
return NextResponse.redirect(syncUrl, { headers })
9696
}
9797

98-
// POST to /api/auth/sync and set the session cookie
99-
async function establishAuthSync (request, searchParams) {
98+
// Exchange verification token for JWT session cookie via POST to /api/auth/sync
99+
async function establishAuthSync (request, searchParams, headers) {
100100
// get the verification token from the search params
101101
const token = searchParams.get('token')
102102
// get the redirectUri from the search params
103103
const redirectUri = searchParams.get('redirectUri') || '/'
104104
// prepare redirect to the redirectUri
105-
const res = NextResponse.redirect(new URL(decodeURIComponent(redirectUri), request.url))
106-
107-
// POST to /api/auth/sync to exchange verification token for session token
108-
const response = await fetch(`${SN_MAIN_DOMAIN.origin}/api/auth/sync`, {
109-
method: 'POST',
110-
headers: {
111-
'Content-Type': 'application/json'
112-
},
113-
body: JSON.stringify({
114-
verificationToken: token
105+
const res = NextResponse.redirect(new URL(decodeURIComponent(redirectUri), request.url), { headers })
106+
107+
try {
108+
// POST to /api/auth/sync to exchange verification token for session token
109+
const response = await fetch(`${SN_MAIN_DOMAIN.origin}/api/auth/sync`, {
110+
method: 'POST',
111+
headers: {
112+
'Content-Type': 'application/json'
113+
},
114+
body: JSON.stringify({
115+
verificationToken: token
116+
})
115117
})
116-
})
117118

118-
// get the session token from the response
119-
const data = await response.json()
120-
if (data.status === 'ERROR') {
121-
// if the response is an error, redirect to the home page
122-
return NextResponse.redirect(new URL('/', request.url))
123-
}
119+
// check if the fetch was successful
120+
if (!response.ok) {
121+
throw new Error(response.status)
122+
}
124123

125-
// set the session cookie
126-
res.cookies.set(SESSION_COOKIE, data.sessionToken, cookieOptions())
127-
return res
124+
// get the session token from the response
125+
const data = await response.json()
126+
if (data.status === 'ERROR') {
127+
throw new Error(data.reason)
128+
}
129+
130+
// set the session cookie
131+
res.cookies.set(SESSION_COOKIE, data.sessionToken, cookieOptions())
132+
return res
133+
} catch (error) {
134+
console.error('[auth sync] cannot establish auth sync:', error.message)
135+
return NextResponse.redirect(new URL('/error', request.url), { headers })
136+
}
128137
}
129138

130139
function getContentReferrer (request, url) {

0 commit comments

Comments
 (0)