Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(admin-ui): add channelTokenKey to AdminUiConfig #2307

Merged
merged 7 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ option.{{< /member-description >}}
setting of the server's `authOptions.authTokenHeaderKey` config
option.{{< /member-description >}}

### channelTokenKey

{{< member-info kind="property" type="string" default="'vendure-token'" >}}

{{< member-description >}}The name of the header which contains the channel token. Should match the
setting of the server's `apiOptions.channelTokenKey` config option.{{< /member-description >}}

### defaultLanguage

{{< member-info kind="property" type="<a href='/typescript-api/common/language-code#languagecode'>LanguageCode</a>" default="<a href='/typescript-api/common/language-code#languagecode'>LanguageCode</a>.en" >}}
Expand Down
10 changes: 7 additions & 3 deletions packages/admin-ui-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MiddlewareConsumer, NestModule } from '@nestjs/common';
import { DEFAULT_AUTH_TOKEN_HEADER_KEY } from '@vendure/common/lib/shared-constants';
import { DEFAULT_AUTH_TOKEN_HEADER_KEY, DEFAULT_CHANNEL_TOKEN_KEY } from '@vendure/common/lib/shared-constants';
import {
AdminUiAppConfig,
AdminUiAppDevModeConfig,
Expand Down Expand Up @@ -251,7 +251,7 @@ export class AdminUiPlugin implements NestModule {
* config object for writing to disk.
*/
private getAdminUiConfig(partialConfig?: Partial<AdminUiConfig>): AdminUiConfig {
const { authOptions } = this.configService;
const { authOptions, apiOptions } = this.configService;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const options = AdminUiPlugin.options!;
const propOrDefault = <Prop extends keyof AdminUiConfig>(
Expand All @@ -261,7 +261,7 @@ export class AdminUiPlugin implements NestModule {
return partialConfig ? (partialConfig as AdminUiConfig)[prop] || defaultVal : defaultVal;
};
return {
adminApiPath: propOrDefault('adminApiPath', this.configService.apiOptions.adminApiPath),
adminApiPath: propOrDefault('adminApiPath', apiOptions.adminApiPath),
apiHost: propOrDefault('apiHost', 'auto'),
apiPort: propOrDefault('apiPort', 'auto'),
tokenMethod: propOrDefault(
Expand All @@ -272,6 +272,10 @@ export class AdminUiPlugin implements NestModule {
'authTokenHeaderKey',
authOptions.authTokenHeaderKey || DEFAULT_AUTH_TOKEN_HEADER_KEY,
),
channelTokenKey: propOrDefault(
'channelTokenKey',
apiOptions.channelTokenKey || DEFAULT_CHANNEL_TOKEN_KEY
),
defaultLanguage: propOrDefault('defaultLanguage', defaultLanguage),
defaultLocale: propOrDefault('defaultLocale', defaultLocale),
availableLanguages: propOrDefault('availableLanguages', defaultAvailableLanguages),
Expand Down
10 changes: 2 additions & 8 deletions packages/admin-ui/src/lib/core/src/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { AdminUiConfig } from '@vendure/common/lib/shared-types';

import { LanguageCode } from './common/generated-types';

let vendureUiConfig: AdminUiConfig | undefined;

export function loadAppConfig(): Promise<void> {
return fetch('./vendure-ui-config.json')
.then(res => res.json())
.then(config => {
vendureUiConfig = config;
});
export async function loadAppConfig(): Promise<void> {
vendureUiConfig = await fetch('./vendure-ui-config.json').then(res => res.json());
}

export function getAppConfig(): AdminUiConfig {
Expand Down
4 changes: 2 additions & 2 deletions packages/admin-ui/src/lib/core/src/data/data.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function createApollo(
fetchAdapter: FetchAdapter,
injector: Injector,
): ApolloClientOptions<any> {
const { adminApiPath, tokenMethod } = getAppConfig();
const { adminApiPath, tokenMethod, channelTokenKey } = getAppConfig();
const serverLocation = getServerLocation();
const apolloCache = new InMemoryCache({
possibleTypes: introspectionResult.possibleTypes,
Expand Down Expand Up @@ -66,7 +66,7 @@ export function createApollo(
const headers: Record<string, string> = {};
const channelToken = localStorageService.get('activeChannelToken');
if (channelToken) {
headers['vendure-token'] = channelToken;
headers[channelTokenKey] = channelToken;
}
if (tokenMethod === 'bearer') {
const authToken = localStorageService.get('authToken');
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/shared-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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'

// An environment variable which is set when the @vendure/create
// script is run. Can be used to modify normal behaviour
Expand Down
11 changes: 9 additions & 2 deletions packages/common/src/shared-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,19 @@ export interface AdminUiConfig {
/**
* @description
* The header used when using the 'bearer' auth method. Should match the
* setting of the server's `authOptions.authTokenHeaderKey` config
* option.
* setting of the server's `authOptions.authTokenHeaderKey` config option.
*
* @default 'vendure-auth-token'
*/
authTokenHeaderKey: string;
/**
* @description
* The name of the header which contains the channel token. Should match the
* setting of the server's `apiOptions.channelTokenKey` config option.
*
* @default 'vendure-token'
*/
channelTokenKey: string;
/**
* @description
* The default language for the Admin UI. Must be one of the
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/config/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
DEFAULT_AUTH_TOKEN_HEADER_KEY,
SUPER_ADMIN_USER_IDENTIFIER,
SUPER_ADMIN_USER_PASSWORD,
DEFAULT_CHANNEL_TOKEN_KEY,
} from '@vendure/common/lib/shared-constants';

import { TypeORMHealthCheckStrategy } from '../health-check/typeorm-health-check-strategy';
Expand Down Expand Up @@ -71,7 +72,7 @@ export const defaultConfig: RuntimeVendureConfig = {
shopApiDebug: false,
shopListQueryLimit: 100,
shopApiValidationRules: [],
channelTokenKey: 'vendure-token',
channelTokenKey: DEFAULT_CHANNEL_TOKEN_KEY,
cors: {
origin: true,
credentials: true,
Expand Down