Skip to content

Commit

Permalink
refactor(core): Extract logic to configure session cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
gdarchen committed Oct 27, 2023
1 parent 3a1e364 commit 203fd90
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
39 changes: 23 additions & 16 deletions packages/core/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,7 @@ export async function bootstrap(userConfig: Partial<VendureConfig>): Promise<INe
const usingCookie =
tokenMethod === 'cookie' || (Array.isArray(tokenMethod) && tokenMethod.includes('cookie'));
if (usingCookie) {
const { cookieOptions } = config.authOptions;
app.use(
cookieSession({
...cookieOptions,
name: typeof cookieOptions?.name === 'string' ? cookieOptions.name : DEFAULT_COOKIE_NAME,
}),
);

// If the Admin API and Shop API should have specific cookies names
if (typeof cookieOptions?.name === 'object') {
const shopApiCookieName = cookieOptions.name.shop;
const adminApiCookieName = cookieOptions.name.admin;
const { shopApiPath, adminApiPath } = config.apiOptions;
app.use(`/${shopApiPath}`, cookieSession({ ...cookieOptions, name: shopApiCookieName }));
app.use(`/${adminApiPath}`, cookieSession({ ...cookieOptions, name: adminApiCookieName }));
}
configureSessionCookies(app, config);
}
const earlyMiddlewares = middleware.filter(mid => mid.beforeListen);
earlyMiddlewares.forEach(mid => {
Expand Down Expand Up @@ -344,3 +329,25 @@ async function validateDbTablesForWorker(worker: INestApplicationContext) {
reject('Could not validate DB table structure. Aborting bootstrap.');
});
}

export function configureSessionCookies(
app: INestApplication,
userConfig: Readonly<RuntimeVendureConfig>,
): void {
const { cookieOptions } = userConfig.authOptions;
app.use(
cookieSession({
...cookieOptions,
name: typeof cookieOptions?.name === 'string' ? cookieOptions.name : DEFAULT_COOKIE_NAME,
}),
);

// If the Admin API and Shop API should have specific cookies names
if (typeof cookieOptions?.name === 'object') {
const shopApiCookieName = cookieOptions.name.shop;
const adminApiCookieName = cookieOptions.name.admin;
const { shopApiPath, adminApiPath } = userConfig.apiOptions;
app.use(`/${shopApiPath}`, cookieSession({ ...cookieOptions, name: shopApiCookieName }));
app.use(`/${adminApiPath}`, cookieSession({ ...cookieOptions, name: adminApiCookieName }));
}
}
27 changes: 2 additions & 25 deletions packages/testing/src/test-server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DEFAULT_COOKIE_NAME } from '@vendure/common/lib/shared-constants';
import { DefaultLogger, JobQueueService, Logger, VendureConfig } from '@vendure/core';
import { preBootstrapConfig } from '@vendure/core/dist/bootstrap';
import cookieSession from 'cookie-session';
import { preBootstrapConfig, configureSessionCookies } from '@vendure/core/dist/bootstrap';

import { populateForTesting } from './data-population/populate-for-testing';
import { getInitializerFor } from './initializers/initializers';
Expand Down Expand Up @@ -122,28 +120,7 @@ export class TestServer {
const usingCookie =
tokenMethod === 'cookie' || (Array.isArray(tokenMethod) && tokenMethod.includes('cookie'));
if (usingCookie) {
const { cookieOptions } = config.authOptions;
app.use(
cookieSession({
...cookieOptions,
name:
typeof cookieOptions?.name === 'string'
? cookieOptions.name
: DEFAULT_COOKIE_NAME,
}),
);

// If the Admin API and Shop API should have specific cookies names
if (typeof cookieOptions?.name === 'object') {
const shopApiCookieName = cookieOptions.name.shop;
const adminApiCookieName = cookieOptions.name.admin;
const { shopApiPath, adminApiPath } = config.apiOptions;
app.use(`/${shopApiPath}`, cookieSession({ ...cookieOptions, name: shopApiCookieName }));
app.use(
`/${adminApiPath}`,
cookieSession({ ...cookieOptions, name: adminApiCookieName }),
);
}
configureSessionCookies(config);
}
const earlyMiddlewares = config.apiOptions.middleware.filter(mid => mid.beforeListen);
earlyMiddlewares.forEach(mid => {
Expand Down

0 comments on commit 203fd90

Please sign in to comment.