|
|
@@ -1,4 +1,5 @@
|
|
|
import { MiddlewareConsumer, Module, NestModule, OnApplicationShutdown } from '@nestjs/common';
|
|
|
+import cookieSession from 'cookie-session';
|
|
|
|
|
|
import { ApiModule } from './api/api.module';
|
|
|
import { Middleware, MiddlewareHandler } from './common';
|
|
|
@@ -26,18 +27,44 @@ import { ServiceModule } from './service/service.module';
|
|
|
],
|
|
|
})
|
|
|
export class AppModule implements NestModule, OnApplicationShutdown {
|
|
|
- constructor(private configService: ConfigService, private i18nService: I18nService) {}
|
|
|
+ constructor(
|
|
|
+ private configService: ConfigService,
|
|
|
+ private i18nService: I18nService,
|
|
|
+ ) {}
|
|
|
|
|
|
configure(consumer: MiddlewareConsumer) {
|
|
|
const { adminApiPath, shopApiPath, middleware } = this.configService.apiOptions;
|
|
|
+ const { cookieOptions } = this.configService.authOptions;
|
|
|
+
|
|
|
const i18nextHandler = this.i18nService.handle();
|
|
|
const defaultMiddleware: Middleware[] = [
|
|
|
{ handler: i18nextHandler, route: adminApiPath },
|
|
|
{ handler: i18nextHandler, route: shopApiPath },
|
|
|
];
|
|
|
+
|
|
|
const allMiddleware = defaultMiddleware.concat(middleware);
|
|
|
+
|
|
|
+ // If the Admin API and Shop API should have specific cookies names, we need to create separate cookie sessions
|
|
|
+ if (typeof cookieOptions?.name === 'object') {
|
|
|
+ const shopApiCookieName = cookieOptions.name.shop;
|
|
|
+ const adminApiCookieName = cookieOptions.name.admin;
|
|
|
+ allMiddleware.push({
|
|
|
+ handler: cookieSession({ ...cookieOptions, name: adminApiCookieName }),
|
|
|
+ route: adminApiPath,
|
|
|
+ });
|
|
|
+ allMiddleware.push({
|
|
|
+ handler: cookieSession({ ...cookieOptions, name: shopApiCookieName }),
|
|
|
+ route: shopApiPath,
|
|
|
+ });
|
|
|
+ allMiddleware.push({
|
|
|
+ handler: cookieSession({ ...cookieOptions, name: shopApiCookieName }),
|
|
|
+ route: '/',
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
const consumableMiddlewares = allMiddleware.filter(mid => !mid.beforeListen);
|
|
|
const middlewareByRoute = this.groupMiddlewareByRoute(consumableMiddlewares);
|
|
|
+
|
|
|
for (const [route, handlers] of Object.entries(middlewareByRoute)) {
|
|
|
consumer.apply(...handlers).forRoutes(route);
|
|
|
}
|