@@ -37,10 +37,10 @@ async function customDomainMiddleware (request, domain, subName) {
37
37
// if the user is trying to login or signup, redirect to the Auth Sync API
38
38
if ( pathname . startsWith ( '/login' ) || pathname . startsWith ( '/signup' ) ) {
39
39
const signup = pathname . startsWith ( '/signup' )
40
- return redirectToAuthSync ( searchParams , domain , signup )
40
+ return redirectToAuthSync ( searchParams , domain , signup , headers )
41
41
}
42
42
// 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 )
44
44
45
45
// Territory URLs
46
46
// if sub param exists and doesn't match the domain's subname, update it
@@ -73,7 +73,7 @@ async function customDomainMiddleware (request, domain, subName) {
73
73
}
74
74
75
75
// redirect to the Auth Sync API
76
- async function redirectToAuthSync ( searchParams , domain , signup ) {
76
+ async function redirectToAuthSync ( searchParams , domain , signup , headers ) {
77
77
const syncUrl = new URL ( '/api/auth/sync' , SN_MAIN_DOMAIN )
78
78
syncUrl . searchParams . set ( 'domain' , domain )
79
79
@@ -92,39 +92,48 @@ async function redirectToAuthSync (searchParams, domain, signup) {
92
92
syncUrl . searchParams . set ( 'redirectUri' , redirectUri )
93
93
}
94
94
95
- return NextResponse . redirect ( syncUrl )
95
+ return NextResponse . redirect ( syncUrl , { headers } )
96
96
}
97
97
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 ) {
100
100
// get the verification token from the search params
101
101
const token = searchParams . get ( 'token' )
102
102
// get the redirectUri from the search params
103
103
const redirectUri = searchParams . get ( 'redirectUri' ) || '/'
104
104
// 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
+ } )
115
117
} )
116
- } )
117
118
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
+ }
124
123
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
+ }
128
137
}
129
138
130
139
function getContentReferrer ( request , url ) {
0 commit comments