From 188fc893c832ba1f875ed96879aa223a6cbfa468 Mon Sep 17 00:00:00 2001 From: Gautier Darchen Date: Wed, 19 Jun 2024 17:22:49 +0200 Subject: [PATCH] fix(cookies): Correctly apply global and per-route middlewares --- packages/core/src/app.module.ts | 4 ---- packages/core/src/bootstrap.ts | 19 +++++++++---------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/packages/core/src/app.module.ts b/packages/core/src/app.module.ts index fdb0a69537..463ee4f884 100644 --- a/packages/core/src/app.module.ts +++ b/packages/core/src/app.module.ts @@ -56,10 +56,6 @@ export class AppModule implements NestModule, OnApplicationShutdown { handler: cookieSession({ ...cookieOptions, name: shopApiCookieName }), route: shopApiPath, }); - allMiddleware.push({ - handler: cookieSession({ ...cookieOptions, name: shopApiCookieName }), - route: '/', - }); } const consumableMiddlewares = allMiddleware.filter(mid => !mid.beforeListen); diff --git a/packages/core/src/bootstrap.ts b/packages/core/src/bootstrap.ts index d55860ddd8..99cb60b22e 100644 --- a/packages/core/src/bootstrap.ts +++ b/packages/core/src/bootstrap.ts @@ -412,14 +412,13 @@ export function configureSessionCookies( ): void { const { cookieOptions } = userConfig.authOptions; - // If the Admin API and Shop API should have the same cookie name - // Else, the specific cookie middlewares are handled in the 'AppModule#configure' method - if (typeof cookieOptions?.name === 'string' || cookieOptions?.name === undefined) { - app.use( - cookieSession({ - ...cookieOptions, - name: cookieOptions?.name ?? DEFAULT_COOKIE_NAME, - }), - ); - } + // Globally set the cookie session middleware + const cookieName = + typeof cookieOptions?.name !== 'string' ? cookieOptions.name?.shop : cookieOptions.name; + app.use( + cookieSession({ + ...cookieOptions, + name: cookieName ?? DEFAULT_COOKIE_NAME, + }), + ); }