Skip to content

Commit

Permalink
feat(core): Enable setting different cookie name for Shop & Admin API (
Browse files Browse the repository at this point in the history
  • Loading branch information
gdarchen authored Oct 27, 2023
1 parent 2b40eec commit ae91650
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/common/src/shared-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const CUSTOMER_ROLE_CODE = '__customer_role__';
export const CUSTOMER_ROLE_DESCRIPTION = 'Customer';
export const ROOT_COLLECTION_NAME = '__root_collection__';
export const DEFAULT_AUTH_TOKEN_HEADER_KEY = 'vendure-auth-token';
export const DEFAULT_CHANNEL_TOKEN_KEY = 'vendure-token'
export const DEFAULT_COOKIE_NAME = 'session';
export const DEFAULT_CHANNEL_TOKEN_KEY = 'vendure-token';

// An environment variable which is set when the @vendure/create
// script is run. Can be used to modify normal behaviour
Expand Down
26 changes: 24 additions & 2 deletions packages/core/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { INestApplication, INestApplicationContext } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { getConnectionToken } from '@nestjs/typeorm';
import { DEFAULT_COOKIE_NAME } from '@vendure/common/lib/shared-constants';
import { Type } from '@vendure/common/lib/shared-types';
import cookieSession = require('cookie-session');
import { satisfies } from 'semver';
Expand Down Expand Up @@ -64,8 +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));
configureSessionCookies(app, config);
}
const earlyMiddlewares = middleware.filter(mid => mid.beforeListen);
earlyMiddlewares.forEach(mid => {
Expand Down Expand Up @@ -329,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 }));
}
}
6 changes: 4 additions & 2 deletions packages/core/src/config/vendure-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,13 @@ export interface ApiOptions {
export interface CookieOptions {
/**
* @description
* The name of the cookie to set.
* The name of the cookies to set.
* If set to a string, both cookies for the Admin API and Shop API will have the same name.
* If set as an object, it makes it possible to give different names to the Admin API and the Shop API cookies
*
* @default 'session'
*/
name?: string;
name?: string | { shop: string; admin: string };

/**
* @description
Expand Down
6 changes: 2 additions & 4 deletions packages/testing/src/test-server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
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 @@ -121,8 +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));
configureSessionCookies(app, config);
}
const earlyMiddlewares = config.apiOptions.middleware.filter(mid => mid.beforeListen);
earlyMiddlewares.forEach(mid => {
Expand Down

0 comments on commit ae91650

Please sign in to comment.