Bläddra i källkod

fix(server): Handle cookie session in shop api

Michael Bromley 7 år sedan
förälder
incheckning
2063e803fe
1 ändrade filer med 10 tillägg och 9 borttagningar
  1. 10 9
      server/src/app.module.ts

+ 10 - 9
server/src/app.module.ts

@@ -18,23 +18,24 @@ export class AppModule implements NestModule {
     constructor(private configService: ConfigService, private i18nService: I18nService) {}
     constructor(private configService: ConfigService, private i18nService: I18nService) {}
 
 
     configure(consumer: MiddlewareConsumer) {
     configure(consumer: MiddlewareConsumer) {
+        const { adminApiPath, shopApiPath } = this.configService;
+
         // tslint:disable-next-line:no-floating-promises
         // tslint:disable-next-line:no-floating-promises
         validateCustomFieldsConfig(this.configService.customFields);
         validateCustomFieldsConfig(this.configService.customFields);
 
 
         const i18nextHandler = this.i18nService.handle();
         const i18nextHandler = this.i18nService.handle();
         const defaultMiddleware: Array<{ handler: RequestHandler; route?: string }> = [
         const defaultMiddleware: Array<{ handler: RequestHandler; route?: string }> = [
-            { handler: i18nextHandler, route: this.configService.adminApiPath },
-            { handler: i18nextHandler, route: this.configService.shopApiPath },
+            { handler: i18nextHandler, route: adminApiPath },
+            { handler: i18nextHandler, route: shopApiPath },
         ];
         ];
         if (this.configService.authOptions.tokenMethod === 'cookie') {
         if (this.configService.authOptions.tokenMethod === 'cookie') {
-            defaultMiddleware.push({
-                handler: cookieSession({
-                    name: 'session',
-                    secret: this.configService.authOptions.sessionSecret,
-                    httpOnly: true,
-                }),
-                route: this.configService.adminApiPath,
+            const cookieHandler = cookieSession({
+                name: 'session',
+                secret: this.configService.authOptions.sessionSecret,
+                httpOnly: true,
             });
             });
+            defaultMiddleware.push({ handler: cookieHandler, route: adminApiPath });
+            defaultMiddleware.push({ handler: cookieHandler, route: shopApiPath });
         }
         }
         const allMiddleware = defaultMiddleware.concat(this.configService.middleware);
         const allMiddleware = defaultMiddleware.concat(this.configService.middleware);
         const middlewareByRoute = this.groupMiddlewareByRoute(allMiddleware);
         const middlewareByRoute = this.groupMiddlewareByRoute(allMiddleware);