diff --git a/packages/core/types/src/auth/service.ts b/packages/core/types/src/auth/service.ts index 88ff60805c..79f2eeb2bc 100644 --- a/packages/core/types/src/auth/service.ts +++ b/packages/core/types/src/auth/service.ts @@ -1,14 +1,16 @@ +import { FindConfig } from "../common" +import { IModuleService } from "../modules-sdk" +import { Context } from "../shared-context" import { AuthIdentityDTO, AuthenticationInput, AuthenticationResponse, CreateAuthIdentityDTO, + CreateProviderIdentityDTO, FilterableAuthIdentityProps, + ProviderIdentityDTO, UpdateAuthIdentityDTO, } from "./common" -import { Context } from "../shared-context" -import { FindConfig } from "../common" -import { IModuleService } from "../modules-sdk" /** * The main service interface for the Auth Module. @@ -274,4 +276,49 @@ export interface IAuthModuleService extends IModuleService { * await authModuleService.deleteAuthIdentities(["authusr_123", "authusr_321"]) */ deleteAuthIdentities(ids: string[], sharedContext?: Context): Promise + + /** + * This method creates provider identities. + * + * @param {CreateProviderIdentityDTO[]} data - The provider identities to be created. + * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. + * @returns {Promise} The created provider identities. + * + * @example + * const providerIdentities = await authModuleService.createProviderIdentities([ + * { + * provider: "emailpass", + * entity_id: "user@example.com", + * auth_identity_id: "uid_1" + * }, + * { + * provider: "github", + * entity_id: "github_handle", + * auth_identity_id: "uid_1" + * }, + * ]) + */ + createProviderIdentities( + data: CreateProviderIdentityDTO[], + sharedContext?: Context + ): Promise + + /** + * This method creates a provider identity. + * + * @param {CreateProviderIdentityDTO} data - The provider identity to be created. + * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. + * @returns {Promise} The created provider identity. + * + * @example + * const providerIdentity = await authModuleService.createProviderIdentities({ + * provider: "github", + * entity_id: "github_handle", + * auth_identity_id: "uid_1" + * }) + */ + createProviderIdentities( + data: CreateProviderIdentityDTO, + sharedContext?: Context + ): Promise } diff --git a/packages/modules/auth/integration-tests/__tests__/auth-module-service/auth-identity.spec.ts b/packages/modules/auth/integration-tests/__tests__/auth-module-service/auth-identity.spec.ts index 3a1de6e87c..613aa053a2 100644 --- a/packages/modules/auth/integration-tests/__tests__/auth-module-service/auth-identity.spec.ts +++ b/packages/modules/auth/integration-tests/__tests__/auth-module-service/auth-identity.spec.ts @@ -1,7 +1,7 @@ import { IAuthModuleService } from "@medusajs/types" -import { createAuthIdentities } from "../../__fixtures__/auth-identity" -import { moduleIntegrationTestRunner } from "medusa-test-utils" import { Modules } from "@medusajs/utils" +import { moduleIntegrationTestRunner } from "medusa-test-utils" +import { createAuthIdentities } from "../../__fixtures__/auth-identity" jest.setTimeout(30000) @@ -259,6 +259,66 @@ moduleIntegrationTestRunner({ ) }) }) + + describe("createProviderIdentity", () => { + it("should create a providerIdentity successfully", async () => { + let authIdentity = await service.retrieveAuthIdentity("test-id", { + relations: ["provider_identities"], + }) + + expect(authIdentity).toEqual( + expect.not.objectContaining({ + provider_identities: [ + expect.objectContaining({ provider: "manual" }), + expect.objectContaining({ provider: "github" }), + ], + }) + ) + + await service.createProviderIdentities({ + id: "test", + entity_id: "christian@medusajs.com", + provider: "github", + auth_identity_id: authIdentity.id, + }) + + authIdentity = await service.retrieveAuthIdentity("test-id", { + relations: ["provider_identities"], + }) + + expect(authIdentity).toEqual( + expect.objectContaining({ + provider_identities: [ + expect.objectContaining({ provider: "manual" }), + expect.objectContaining({ provider: "github" }), + ], + }) + ) + }) + + it("should list authIdentities by newly created provider", async () => { + await service.createProviderIdentities([ + { + id: "test", + entity_id: "christian@medusajs.com", + provider: "github", + auth_identity_id: "test-id", + }, + ]) + + const authIdentities = await service.listAuthIdentities({ + provider_identities: { + provider: "github", + }, + }) + + expect(authIdentities).toEqual([ + expect.objectContaining({ + id: "test-id", + }), + ]) + }) + }) }) }, }) diff --git a/packages/modules/auth/src/services/auth-module.ts b/packages/modules/auth/src/services/auth-module.ts index 2a754c64e5..e271c4d58d 100644 --- a/packages/modules/auth/src/services/auth-module.ts +++ b/packages/modules/auth/src/services/auth-module.ts @@ -121,6 +121,36 @@ export default class AuthModuleService return Array.isArray(data) ? serializedUsers : serializedUsers[0] } + // @ts-expect-error + createProviderIdentities( + data: AuthTypes.CreateProviderIdentityDTO[], + sharedContext?: Context + ): Promise + + createProviderIdentities( + data: AuthTypes.CreateProviderIdentityDTO, + sharedContext?: Context + ): Promise + + @InjectManager("baseRepository_") + async createProviderIdentities( + data: + | AuthTypes.CreateProviderIdentityDTO[] + | AuthTypes.CreateProviderIdentityDTO, + @MedusaContext() sharedContext: Context = {} + ): Promise { + const providerIdentities = await this.providerIdentityService_.create( + data, + sharedContext + ) + + return await this.baseRepository_.serialize< + AuthTypes.ProviderIdentityDTO[] + >(providerIdentities, { + populate: true, + }) + } + async authenticate( provider: string, authenticationData: AuthenticationInput