@@ -34,13 +34,19 @@ async function customDomainMiddleware (request, domain, subName) {
34
34
console . log ( '[domains] searchParams' , searchParams ) // TEST
35
35
36
36
// Auth Sync
37
- // if the user is trying to login or signup, redirect to the Auth Sync API
37
+ // CSRF protection
38
+ // retrieve the csrfToken from the cookie
39
+ const csrfCookie = request . cookies . get ( '__Host-next-auth.csrf-token' )
40
+ // csrf is stored as token|hash, we only need the token
41
+ const csrfToken = csrfCookie ? csrfCookie . value . split ( '|' ) [ 0 ] : null
42
+
43
+ // A: the user is trying to login or signup, redirect to the Auth Sync API
38
44
if ( pathname . startsWith ( '/login' ) || pathname . startsWith ( '/signup' ) ) {
39
45
const signup = pathname . startsWith ( '/signup' )
40
- return redirectToAuthSync ( request , searchParams , domain , signup , headers )
46
+ return redirectToAuthSync ( searchParams , domain , csrfToken , signup , headers )
41
47
}
42
- // if we have a verification token, exchange it for a session token
43
- if ( searchParams . has ( 'synctoken' ) ) return establishAuthSync ( request , searchParams , headers )
48
+ // B: if we have a verification token, exchange it for a session token
49
+ if ( searchParams . has ( 'synctoken' ) ) return establishAuthSync ( request , searchParams , csrfToken , headers )
44
50
45
51
// Territory URLs
46
52
// if sub param exists and doesn't match the domain's subname, update it
@@ -73,17 +79,12 @@ async function customDomainMiddleware (request, domain, subName) {
73
79
}
74
80
75
81
// redirect to the Auth Sync API
76
- async function redirectToAuthSync ( request , searchParams , domain , signup , headers ) {
82
+ async function redirectToAuthSync ( searchParams , domain , csrfToken , signup , headers ) {
83
+ // bail if we don't have a csrfToken
84
+ if ( ! csrfToken ) return NextResponse . redirect ( '/error' , { headers } )
85
+
77
86
const syncUrl = new URL ( '/api/auth/sync' , SN_MAIN_DOMAIN )
78
87
syncUrl . searchParams . set ( 'domain' , domain )
79
-
80
- // -- CSRF protection --
81
- // retrieve the csrfToken from the cookie
82
- const csrfCookie = request . cookies . get ( '__Host-next-auth.csrf-token' )
83
- // csrf is stored as token|hash, we only need the token
84
- const csrfToken = csrfCookie ? csrfCookie . value . split ( '|' ) [ 0 ] : null
85
- // bail if we don't have a csrfToken
86
- if ( ! csrfToken ) return NextResponse . redirect ( new URL ( '/error' , request . url ) , { headers } )
87
88
// store the csrfToken in the search params
88
89
syncUrl . searchParams . set ( 'state' , csrfToken )
89
90
@@ -106,21 +107,16 @@ async function redirectToAuthSync (request, searchParams, domain, signup, header
106
107
}
107
108
108
109
// Exchange verification token for JWT session cookie via POST to /api/auth/sync
109
- async function establishAuthSync ( request , searchParams , headers ) {
110
+ async function establishAuthSync ( request , searchParams , csrfToken , headers ) {
111
+ // bail if we don't have a csrfToken
112
+ if ( ! csrfToken ) return NextResponse . redirect ( '/error' , { headers } )
110
113
// get the verification token from the search params
111
114
const token = searchParams . get ( 'synctoken' )
112
115
// get the redirectUri from the search params
113
116
const redirectUri = searchParams . get ( 'redirectUri' ) || '/'
114
117
// prepare redirect to the redirectUri
115
118
const res = NextResponse . redirect ( new URL ( decodeURIComponent ( redirectUri ) , request . url ) , { headers } )
116
119
117
- // -- CSRF protection --
118
- const csrfCookie = request . cookies . get ( '__Host-next-auth.csrf-token' )
119
- // csrf is stored as token|hash, we only need the token
120
- const csrfToken = csrfCookie ? csrfCookie . value . split ( '|' ) [ 0 ] : null
121
- // bail if we don't have a csrfToken
122
- if ( ! csrfToken ) return NextResponse . redirect ( new URL ( '/error' , request . url ) , { headers } )
123
-
124
120
try {
125
121
// POST to /api/auth/sync to exchange verification token for session token
126
122
const response = await fetch ( `${ SN_MAIN_DOMAIN . origin } /api/auth/sync` , {
0 commit comments