diff --git a/packages/authentication/integration-tests/__tests__/services/module/providers.spec.ts b/packages/authentication/integration-tests/__tests__/services/module/providers.spec.ts index 607e6d0801c9d..3a49d6c3b0217 100644 --- a/packages/authentication/integration-tests/__tests__/services/module/providers.spec.ts +++ b/packages/authentication/integration-tests/__tests__/services/module/providers.spec.ts @@ -23,6 +23,10 @@ describe("AuthenticationModuleService - AuthProvider", () => { schema: process.env.MEDUSA_PRICING_DB_SCHEMA, }, }) + + if(service.__hooks?.onApplicationStart) { + await service.__hooks.onApplicationStart() + } }) afterEach(async () => { diff --git a/packages/authentication/src/loaders/providers.ts b/packages/authentication/src/loaders/providers.ts index c9ff2ee18fa52..0ffa6eee8f979 100644 --- a/packages/authentication/src/loaders/providers.ts +++ b/packages/authentication/src/loaders/providers.ts @@ -2,13 +2,10 @@ import * as defaultProviders from "@providers" import { LoaderOptions, ModulesSdkTypes } from "@medusajs/types" -import { AuthProviderService } from "@services" -import { ServiceTypes } from "@types" -import { asClass } from "awilix" +import { AwilixContainer, ClassOrFunctionReturning, Resolver, asClass, asFunction, asValue } from "awilix" export default async ({ container, - options, }: LoaderOptions< | ModulesSdkTypes.ModuleServiceInitializeOptions | ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions @@ -19,40 +16,22 @@ export default async ({ const providersToLoad = Object.values(defaultProviders) - const authProviderService: AuthProviderService = container.resolve( - "authProviderService" - ) - let providers - - try { - providers = await authProviderService.list({ - provider: providersToLoad.map((p) => p.PROVIDER), - }) - } catch (error) { - if (error.name === "TableNotFoundException") { - // we are running loaders in migrations (or fail at a later point) - return - } - } - - const loadedProviders = new Map(providers.map((p) => [p.provider, p])) - - const providersToCreate: ServiceTypes.CreateAuthProviderDTO[] = [] - for (const provider of providersToLoad) { container.register({ [`auth_provider_${provider.PROVIDER}`]: asClass(provider).singleton(), }) + } - if (loadedProviders.has(provider.PROVIDER)) { - continue - } + container.register({ + [`auth_providers`]: asArray(providersToLoad), + }) +} - providersToCreate.push({ - provider: provider.PROVIDER, - name: provider.DISPLAY_NAME, - }) +function asArray( + resolvers: (ClassOrFunctionReturning | Resolver)[] +): { resolve: (container: AwilixContainer) => unknown[] } { + return { + resolve: (container: AwilixContainer) => + resolvers.map((resolver) => container.build(resolver)), } - - await authProviderService.create(providersToCreate) } diff --git a/packages/authentication/src/services/authentication-module.ts b/packages/authentication/src/services/authentication-module.ts index 86ed10be7b617..7638dc8595fc8 100644 --- a/packages/authentication/src/services/authentication-module.ts +++ b/packages/authentication/src/services/authentication-module.ts @@ -14,6 +14,7 @@ import { AuthProvider, AuthUser } from "@models" import { joinerConfig } from "../joiner-config" import { AuthProviderService, AuthUserService } from "@services" + import { InjectManager, InjectTransactionManager, @@ -29,6 +30,7 @@ import { FilterableAuthUserProps, UpdateAuthUserDTO, } from "@medusajs/types/dist/authentication/common" +import { ServiceTypes } from "@types" type InjectedDependencies = { baseRepository: DAL.RepositoryService @@ -45,6 +47,33 @@ export default class AuthenticationModuleService< return joinerConfig } + __hooks = { + onApplicationStart: async () => { + const providersToLoad = this.__container__["auth_providers"] + + const providers = await this.authProviderService_.list({ + provider: providersToLoad.map((p) => p.provider), + }) + + const loadedProvidersMap = new Map(providers.map((p) => [p.provider, p])) + + const providersToCreate: ServiceTypes.CreateAuthProviderDTO[] = [] + + for (const provider of providersToLoad) { + if (loadedProvidersMap.has(provider.provider)) { + continue + } + + providersToCreate.push({ + provider: provider.provider, + name: provider.displayName, + }) + } + + await this.authProviderService_.create(providersToCreate) + }, + } + protected __container__: MedusaContainer protected baseRepository_: DAL.RepositoryService @@ -351,10 +380,8 @@ export default class AuthenticationModuleService< ): AbstractAuthenticationModuleProvider { let containerProvider: AbstractAuthenticationModuleProvider try { - console.log("containerProvider") containerProvider = this.__container__[`auth_provider_${provider}`] } catch (error) { - console.log(error) throw new MedusaError( MedusaError.Types.NOT_FOUND, `AuthenticationProvider with for provider: ${provider} wasn't registered in the module. Have you configured your options correctly?` @@ -369,18 +396,16 @@ export default class AuthenticationModuleService< provider: string, authenticationData: Record, @MedusaContext() sharedContext: Context = {} - ): Promise { - let registeredProvider - console.log("hello") - try { + ): Promise { + let registeredProvider + + try { await this.retrieveAuthProvider(provider, {}) - console.log(registeredProvider) registeredProvider = this.getRegisteredAuthenticationProvider(provider) - + return await registeredProvider.authenticate(authenticationData) - } catch (error) { - console.log(JSON.stringify(error, null, 2)) + } catch (error) { return { success: false, error: error.message } } } diff --git a/packages/types/src/authentication/provider.ts b/packages/types/src/authentication/provider.ts index 7c2e0bafd3e76..b0be42d02b0d2 100644 --- a/packages/types/src/authentication/provider.ts +++ b/packages/types/src/authentication/provider.ts @@ -4,6 +4,22 @@ export abstract class AbstractAuthenticationModuleProvider { public static PROVIDER: string public static DISPLAY_NAME: string + public get provider() { + return (this.constructor as Function & { PROVIDER: string}).PROVIDER + } + + public get displayName() { + return (this.constructor as Function & { DISPLAY_NAME: string}).DISPLAY_NAME + } + + public getProvider(): string { + return (this.constructor as Function & { PROVIDER: string}).PROVIDER + } + + public getDisplayName(): string { + return (this.constructor as Function & { DISPLAY_NAME: string, PROVIDER: string}).PROVIDER + } + abstract authenticate( data: Record ): Promise