Sfoglia il codice sorgente

feat(server): Allow default Channel token to be specified in config

Closes #32
Michael Bromley 7 anni fa
parent
commit
d81d51f0f2

+ 1 - 0
server/dev-config.ts

@@ -8,6 +8,7 @@ import { DefaultAssetServerPlugin } from './src/plugin/default-asset-server/defa
  * Config settings used during development
  */
 export const devConfig: VendureConfig = {
+    defaultChannelToken: 'default-channel',
     authOptions: {
         disableAuth: false,
         sessionSecret: 'some-secret',

+ 1 - 0
server/src/config/config.service.mock.ts

@@ -6,6 +6,7 @@ import { EntityIdStrategy, PrimaryKeyType } from './entity-id-strategy/entity-id
 
 export class MockConfigService implements MockClass<ConfigService> {
     authOptions: {};
+    defaultChannelToken: 'channel-token';
     channelTokenKey: 'vendure-token';
     apiPath = 'api';
     port = 3000;

+ 4 - 0
server/src/config/config.service.ts

@@ -33,6 +33,10 @@ export class ConfigService implements VendureConfig {
         return this.activeConfig.authOptions as Required<AuthOptions>;
     }
 
+    get defaultChannelToken(): string | null {
+        return this.activeConfig.defaultChannelToken;
+    }
+
     get channelTokenKey(): string {
         return this.activeConfig.channelTokenKey;
     }

+ 1 - 0
server/src/config/default-config.ts

@@ -17,6 +17,7 @@ import { VendureConfig } from './vendure-config';
  */
 export const defaultConfig: ReadOnlyRequired<VendureConfig> = {
     channelTokenKey: 'vendure-token',
+    defaultChannelToken: null,
     defaultLanguageCode: LanguageCode.en,
     port: API_PORT,
     cors: {

+ 5 - 0
server/src/config/vendure-config.ts

@@ -63,6 +63,11 @@ export interface VendureConfig {
      * the request header or as a query string.
      */
     channelTokenKey?: string;
+    /**
+     * The token for the default channel. If not specified, a token
+     * will be randomly generated.
+     */
+    defaultChannelToken?: string | null;
     /**
      * The default languageCode of the app.
      */

+ 3 - 1
server/src/entity/channel/channel.entity.ts

@@ -8,7 +8,9 @@ import { VendureEntity } from '../base/base.entity';
 export class Channel extends VendureEntity {
     constructor(input?: DeepPartial<Channel>) {
         super(input);
-        this.token = this.generateToken();
+        if (!input || !input.token) {
+            this.token = this.generateToken();
+        }
     }
 
     @Column({ unique: true })

+ 8 - 1
server/src/service/providers/channel.service.ts

@@ -6,6 +6,7 @@ import { Connection } from 'typeorm';
 import { RequestContext } from '../../api/common/request-context';
 import { DEFAULT_LANGUAGE_CODE } from '../../common/constants';
 import { ChannelAware } from '../../common/types/common-types';
+import { ConfigService } from '../../config/config.service';
 import { Channel } from '../../entity/channel/channel.entity';
 import { I18nError } from '../../i18n/i18n-error';
 
@@ -13,7 +14,7 @@ import { I18nError } from '../../i18n/i18n-error';
 export class ChannelService {
     private allChannels: Channel[] = [];
 
-    constructor(@InjectConnection() private connection: Connection) {}
+    constructor(@InjectConnection() private connection: Connection, private configService: ConfigService) {}
 
     /**
      * When the app is bootstrapped, ensure a default Channel exists and populate the
@@ -69,8 +70,10 @@ export class ChannelService {
 
     /**
      * There must always be a default Channel. If none yet exists, this method creates one.
+     * Also ensures the default Channel token matches the defaultChannelToken config setting.
      */
     private async ensureDefaultChannelExists() {
+        const { defaultChannelToken } = this.configService;
         const defaultChannel = await this.connection.getRepository(Channel).findOne({
             where: {
                 code: DEFAULT_CHANNEL_CODE,
@@ -81,8 +84,12 @@ export class ChannelService {
             const newDefaultChannel = new Channel({
                 code: DEFAULT_CHANNEL_CODE,
                 defaultLanguageCode: DEFAULT_LANGUAGE_CODE,
+                token: defaultChannelToken,
             });
             await this.connection.manager.save(newDefaultChannel);
+        } else if (defaultChannelToken && defaultChannel.token !== defaultChannelToken) {
+            defaultChannel.token = defaultChannelToken;
+            await this.connection.manager.save(defaultChannel);
         }
     }
 }