@@ -106,15 +106,23 @@ export function checkMultiAuthCookies (req, res) {
106
106
return true
107
107
}
108
108
109
- function resetMultiAuthCookies ( req , res ) {
109
+ async function resetMultiAuthCookies ( req , res ) {
110
110
const httpOnlyOptions = cookieOptions ( { expires : 0 , maxAge : 0 } )
111
111
const jsOptions = { ...httpOnlyOptions , httpOnly : false }
112
112
113
+ // remove all multi_auth cookies ...
113
114
for ( const key of Object . keys ( req . cookies ) ) {
114
115
if ( ! MULTI_AUTH_REGEXP . test ( key ) ) continue
115
116
const options = MULTI_AUTH_JWT_REGEXP . test ( key ) ? httpOnlyOptions : jsOptions
116
117
res . appendHeader ( 'Set-Cookie' , cookie . serialize ( key , '' , options ) )
117
118
}
119
+
120
+ // ... and reset to initial state if they are logged in
121
+ const token = req . cookies [ SESSION_COOKIE ]
122
+ if ( ! token ) return
123
+
124
+ const decoded = await decodeJWT ( { token, secret : process . env . NEXTAUTH_SECRET } )
125
+ setMultiAuthCookies ( req , res , { ...decoded , jwt : token } )
118
126
}
119
127
120
128
async function refreshMultiAuthCookies ( req , res ) {
@@ -160,7 +168,7 @@ export async function multiAuthMiddleware (req, res) {
160
168
161
169
const ok = checkMultiAuthCookies ( req , res )
162
170
if ( ! ok ) {
163
- resetMultiAuthCookies ( req , res )
171
+ await resetMultiAuthCookies ( req , res )
164
172
return switchSessionCookie ( req )
165
173
}
166
174
0 commit comments