Skip to content

Commit

Permalink
feat(admin-ui-plugin): Automatically configure admin-ui auth method
Browse files Browse the repository at this point in the history
Closes #138
  • Loading branch information
michaelbromley committed Aug 20, 2019
1 parent c31a383 commit fd68d1e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/admin-ui-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DEFAULT_AUTH_TOKEN_HEADER_KEY } from '@vendure/common/lib/shared-constants';
import { AdminUiConfig, Type } from '@vendure/common/lib/shared-types';
import {
AuthOptions,
createProxyHandler,
OnVendureBootstrap,
OnVendureClose,
Expand Down Expand Up @@ -102,9 +104,9 @@ export class AdminUiPlugin implements OnVendureBootstrap, OnVendureClose {
handler: createProxyHandler({ ...this.options, route, label: 'Admin UI' }),
route,
});
const { adminApiPath } = config;
const { adminApiPath, authOptions } = config;
const { apiHost, apiPort } = this.options;
await this.overwriteAdminUiConfig(apiHost || 'auto', apiPort || 'auto', adminApiPath);
await this.overwriteAdminUiConfig(apiHost || 'auto', apiPort || 'auto', adminApiPath, authOptions);
return config;
}

Expand Down Expand Up @@ -132,13 +134,21 @@ export class AdminUiPlugin implements OnVendureBootstrap, OnVendureClose {
host: string | 'auto',
port: number | 'auto',
adminApiPath: string,
authOptions: AuthOptions,
) {
const adminUiConfigPath = path.join(this.getAdminUiPath(), 'vendure-ui-config.json');
const adminUiConfig = await fs.readFile(adminUiConfigPath, 'utf-8');
const config: AdminUiConfig = JSON.parse(adminUiConfig);
let config: AdminUiConfig;
try {
config = JSON.parse(adminUiConfig);
} catch (e) {
throw new Error('[AdminUiPlugin] Could not parse vendure-ui-config.json file:\n' + e.message);
}
config.apiHost = host || 'http://localhost';
config.apiPort = port;
config.adminApiPath = adminApiPath;
config.tokenMethod = authOptions.tokenMethod || 'cookie';
config.authTokenHeaderKey = authOptions.authTokenHeaderKey || DEFAULT_AUTH_TOKEN_HEADER_KEY;
await fs.writeFile(adminUiConfigPath, JSON.stringify(config, null, 2));
}

Expand Down

0 comments on commit fd68d1e

Please sign in to comment.