Revamp auth module to support multiple providers linked to a single auth identity (#7521)
This commit is contained in:
@@ -1,34 +1,36 @@
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { AuthIdentity } from "@models"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export async function createAuthIdentities(
|
||||
manager: SqlEntityManager,
|
||||
service: IAuthModuleService,
|
||||
userData: any[] = [
|
||||
{
|
||||
id: "test-id",
|
||||
entity_id: "test-id",
|
||||
provider: "manual",
|
||||
provider_identities: [
|
||||
{
|
||||
entity_id: "test-id",
|
||||
provider: "manual",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "test-id-1",
|
||||
entity_id: "test-id-1",
|
||||
provider: "manual",
|
||||
provider_identities: [
|
||||
{
|
||||
entity_id: "test-id-1",
|
||||
provider: "manual",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
entity_id: "test-id-2",
|
||||
provider: "store",
|
||||
provider_identities: [
|
||||
{
|
||||
entity_id: "test-id-2",
|
||||
provider: "store",
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
): Promise<AuthIdentity[]> {
|
||||
const authIdentities: AuthIdentity[] = []
|
||||
|
||||
for (const user of userData) {
|
||||
const authIdentity = manager.create(AuthIdentity, user)
|
||||
|
||||
authIdentities.push(authIdentity)
|
||||
}
|
||||
|
||||
await manager.persistAndFlush(authIdentities)
|
||||
|
||||
return authIdentities
|
||||
return await service.create(userData)
|
||||
}
|
||||
|
||||
@@ -23,10 +23,14 @@ export class AuthServiceFixtures extends AbstractAuthModuleProvider {
|
||||
try {
|
||||
authIdentity = await service.retrieve({
|
||||
entity_id: email,
|
||||
provider: this.provider,
|
||||
})
|
||||
|
||||
if (authIdentity.provider_metadata?.password === password) {
|
||||
// The provider has to be present, guaranteed by the retrieve filter above.
|
||||
const providerIdentity = authIdentity.provider_identities?.find(
|
||||
(pi) => pi.provider === this.provider
|
||||
)!
|
||||
|
||||
if (providerIdentity.provider_metadata?.password === password) {
|
||||
return {
|
||||
success: true,
|
||||
authIdentity,
|
||||
@@ -36,7 +40,6 @@ export class AuthServiceFixtures extends AbstractAuthModuleProvider {
|
||||
if (error.type === MedusaError.Types.NOT_FOUND) {
|
||||
const createdAuthIdentity = await service.create({
|
||||
entity_id: email,
|
||||
provider: this.provider,
|
||||
provider_metadata: {
|
||||
password,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user