From 42000fbaaf75745b1d6f72e545b7491b94f49ab8 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 20 May 2020 12:34:42 +0200 Subject: [PATCH] packages/core: refactor AuthConnector options a bit and add docs --- .../implementations/auth/google/GoogleAuth.ts | 2 +- .../DefaultAuthConnector.test.ts | 2 +- .../lib/AuthConnector/DefaultAuthConnector.ts | 29 ++++++++++++++----- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/core/src/api/apis/implementations/auth/google/GoogleAuth.ts b/packages/core/src/api/apis/implementations/auth/google/GoogleAuth.ts index 599ce789db..e3c10d71f7 100644 --- a/packages/core/src/api/apis/implementations/auth/google/GoogleAuth.ts +++ b/packages/core/src/api/apis/implementations/auth/google/GoogleAuth.ts @@ -38,9 +38,9 @@ const SCOPE_PREFIX = 'https://www.googleapis.com/auth/'; class GoogleAuth implements OAuthApi, OpenIdConnectApi { static create(oauthRequestApi: OAuthRequestApi) { const connector = new DefaultAuthConnector({ - providerPath: 'google/', environment: 'dev', provider: { + id: 'google', title: 'Google', icon: GoogleIcon, }, diff --git a/packages/core/src/api/apis/implementations/lib/AuthConnector/DefaultAuthConnector.test.ts b/packages/core/src/api/apis/implementations/lib/AuthConnector/DefaultAuthConnector.test.ts index 4ea3bde015..812ee07add 100644 --- a/packages/core/src/api/apis/implementations/lib/AuthConnector/DefaultAuthConnector.test.ts +++ b/packages/core/src/api/apis/implementations/lib/AuthConnector/DefaultAuthConnector.test.ts @@ -23,9 +23,9 @@ const anyFetch = fetch as any; const defaultOptions = { apiOrigin: 'my-origin', - providerPath: 'my-provider', environment: 'production', provider: { + id: 'my-provider', title: 'My Provider', icon: ProviderIcon, }, diff --git a/packages/core/src/api/apis/implementations/lib/AuthConnector/DefaultAuthConnector.ts b/packages/core/src/api/apis/implementations/lib/AuthConnector/DefaultAuthConnector.ts index bad8ac948b..e9d3918742 100644 --- a/packages/core/src/api/apis/implementations/lib/AuthConnector/DefaultAuthConnector.ts +++ b/packages/core/src/api/apis/implementations/lib/AuthConnector/DefaultAuthConnector.ts @@ -22,12 +22,30 @@ import { AuthConnector } from './types'; const DEFAULT_BASE_PATH = '/api/auth/'; type Options = { + /** + * Origin of auth requests, defaults to location.origin + */ apiOrigin?: string; + /** + * Base path of the auth requests, defaults to /api/auth/ + */ basePath?: string; - providerPath: string; + /** + * Environment hint passed on to auth backend, for example 'production' or 'development' + */ environment: string; - provider: AuthProvider; + /** + * Information about the auth provider to be shown to the user. + * The ID Must match the backend auth plugin configuration, for example 'google'. + */ + provider: AuthProvider & { id: string }; + /** + * API used to instanciate an auth requester. + */ oauthRequestApi: OAuthRequestApi; + /** + * Function used to transform an auth response into the session type. + */ sessionTransform?(response: any): AuthSession | Promise; }; @@ -40,9 +58,8 @@ export class DefaultAuthConnector implements AuthConnector { private readonly apiOrigin: string; private readonly basePath: string; - private readonly providerPath: string; private readonly environment: string; - private readonly provider: AuthProvider; + private readonly provider: AuthProvider & { id: string }; private readonly authRequester: AuthRequester; private readonly sessionTransform: (response: any) => Promise; @@ -50,7 +67,6 @@ export class DefaultAuthConnector const { apiOrigin = window.location.origin, basePath = DEFAULT_BASE_PATH, - providerPath, environment, provider, oauthRequestApi, @@ -64,7 +80,6 @@ export class DefaultAuthConnector this.apiOrigin = apiOrigin; this.basePath = basePath; - this.providerPath = providerPath; this.environment = environment; this.provider = provider; this.sessionTransform = sessionTransform; @@ -141,7 +156,7 @@ export class DefaultAuthConnector env: this.environment, }); - return `${this.apiOrigin}${this.basePath}${this.providerPath}${path}${queryString}`; + return `${this.apiOrigin}${this.basePath}${this.provider.id}${path}${queryString}`; } private buildQueryString(query?: {