@@ -92,7 +92,7 @@ function switchSessionCookie (request) {
92
92
}
93
93
94
94
async function checkMultiAuthCookies ( req , res ) {
95
- if ( ! req . cookies [ MULTI_AUTH_LIST ] || ! req . cookies [ MULTI_AUTH_POINTER ] ) {
95
+ if ( ! req . cookies [ MULTI_AUTH_LIST ] || ! req . cookies [ MULTI_AUTH_POINTER ] || ! req . cookies [ SESSION_COOKIE ] ) {
96
96
return false
97
97
}
98
98
@@ -116,15 +116,23 @@ async function checkMultiAuthCookies (req, res) {
116
116
return true
117
117
}
118
118
119
- function resetMultiAuthCookies ( req , res ) {
119
+ async function resetMultiAuthCookies ( req , res ) {
120
120
const httpOnlyOptions = cookieOptions ( { expires : 0 , maxAge : 0 } )
121
121
const jsOptions = { ...httpOnlyOptions , httpOnly : false }
122
122
123
+ // remove all multi_auth cookies ...
123
124
for ( const key of Object . keys ( req . cookies ) ) {
124
125
if ( ! MULTI_AUTH_REGEXP . test ( key ) ) continue
125
126
const options = MULTI_AUTH_JWT_REGEXP . test ( key ) ? httpOnlyOptions : jsOptions
126
127
res . appendHeader ( 'Set-Cookie' , cookie . serialize ( key , '' , options ) )
127
128
}
129
+
130
+ // ... and reset to initial state if they are logged in
131
+ const token = req . cookies [ SESSION_COOKIE ]
132
+ if ( ! token ) return
133
+
134
+ const decoded = await decodeJWT ( { token, secret : process . env . NEXTAUTH_SECRET } )
135
+ setMultiAuthCookies ( req , res , { ...decoded , jwt : token } )
128
136
}
129
137
130
138
async function refreshMultiAuthCookies ( req , res ) {
@@ -170,7 +178,7 @@ export async function multiAuthMiddleware (req, res) {
170
178
171
179
const ok = await checkMultiAuthCookies ( req , res )
172
180
if ( ! ok ) {
173
- resetMultiAuthCookies ( req , res )
181
+ await resetMultiAuthCookies ( req , res )
174
182
return switchSessionCookie ( req )
175
183
}
176
184
0 commit comments