Skip to content

Commit c14ee46

Browse files
committed
cleanup: get csrfToken cookie once, bail if csrfToken cookie is not present
1 parent 236fb1b commit c14ee46

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

middleware.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,19 @@ async function customDomainMiddleware (request, domain, subName) {
3434
console.log('[domains] searchParams', searchParams) // TEST
3535

3636
// 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
3844
if (pathname.startsWith('/login') || pathname.startsWith('/signup')) {
3945
const signup = pathname.startsWith('/signup')
40-
return redirectToAuthSync(request, searchParams, domain, signup, headers)
46+
return redirectToAuthSync(searchParams, domain, csrfToken, signup, headers)
4147
}
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)
4450

4551
// Territory URLs
4652
// if sub param exists and doesn't match the domain's subname, update it
@@ -73,17 +79,12 @@ async function customDomainMiddleware (request, domain, subName) {
7379
}
7480

7581
// 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+
7786
const syncUrl = new URL('/api/auth/sync', SN_MAIN_DOMAIN)
7887
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 })
8788
// store the csrfToken in the search params
8889
syncUrl.searchParams.set('state', csrfToken)
8990

@@ -106,21 +107,16 @@ async function redirectToAuthSync (request, searchParams, domain, signup, header
106107
}
107108

108109
// 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 })
110113
// get the verification token from the search params
111114
const token = searchParams.get('synctoken')
112115
// get the redirectUri from the search params
113116
const redirectUri = searchParams.get('redirectUri') || '/'
114117
// prepare redirect to the redirectUri
115118
const res = NextResponse.redirect(new URL(decodeURIComponent(redirectUri), request.url), { headers })
116119

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-
124120
try {
125121
// POST to /api/auth/sync to exchange verification token for session token
126122
const response = await fetch(`${SN_MAIN_DOMAIN.origin}/api/auth/sync`, {

0 commit comments

Comments
 (0)