feat: create auth provider identity (#8675)

This commit is contained in:
Christian
2024-08-20 16:38:06 +02:00
committed by GitHub
parent 3706bf51af
commit 29830f0077
3 changed files with 142 additions and 5 deletions
@@ -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<IAuthModuleService>({
)
})
})
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",
}),
])
})
})
})
},
})
@@ -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<AuthTypes.ProviderIdentityDTO[]>
createProviderIdentities(
data: AuthTypes.CreateProviderIdentityDTO,
sharedContext?: Context
): Promise<AuthTypes.ProviderIdentityDTO>
@InjectManager("baseRepository_")
async createProviderIdentities(
data:
| AuthTypes.CreateProviderIdentityDTO[]
| AuthTypes.CreateProviderIdentityDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<AuthTypes.ProviderIdentityDTO | AuthTypes.ProviderIdentityDTO[]> {
const providerIdentities = await this.providerIdentityService_.create(
data,
sharedContext
)
return await this.baseRepository_.serialize<
AuthTypes.ProviderIdentityDTO[]
>(providerIdentities, {
populate: true,
})
}
async authenticate(
provider: string,
authenticationData: AuthenticationInput