Revamp auth module to support multiple providers linked to a single auth identity (#7521)
This commit is contained in:
@@ -1,50 +1,45 @@
|
||||
import {
|
||||
BeforeCreate,
|
||||
Collection,
|
||||
Entity,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
OneToMany,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
Unique,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import { generateEntityId } from "@medusajs/utils"
|
||||
|
||||
type OptionalFields = "provider_metadata" | "app_metadata" | "user_metadata"
|
||||
import ProviderIdentity from "./provider-identity"
|
||||
|
||||
@Entity()
|
||||
@Unique({
|
||||
properties: ["provider", "entity_id"],
|
||||
name: "IDX_auth_identity_provider_entity_id",
|
||||
})
|
||||
export default class AuthIdentity {
|
||||
[OptionalProps]: OptionalFields
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id!: string
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
entity_id: string
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
provider: string
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
user_metadata: Record<string, unknown> | null
|
||||
@OneToMany(() => ProviderIdentity, (o) => o.auth_identity)
|
||||
provider_identities = new Collection<ProviderIdentity>(this)
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
app_metadata: Record<string, unknown> | null
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
provider_metadata: Record<string, unknown> | null = null
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
created_at: Date
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
onUpdate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
updated_at: Date
|
||||
|
||||
@BeforeCreate()
|
||||
@OnInit()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "authid")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "authid")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export { default as AuthIdentity } from "./auth-identity"
|
||||
export { default as ProviderIdentity } from "./provider-identity"
|
||||
|
||||
84
packages/modules/auth/src/models/provider-identity.ts
Normal file
84
packages/modules/auth/src/models/provider-identity.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import {
|
||||
BeforeCreate,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
OnInit,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import {
|
||||
createPsqlIndexStatementHelper,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
import AuthIdentity from "./auth-identity"
|
||||
|
||||
const providerEntityIdIndexName = "IDX_provider_identity_provider_entity_id"
|
||||
const providerEntityIdIndexStatement = createPsqlIndexStatementHelper({
|
||||
name: providerEntityIdIndexName,
|
||||
tableName: "provider_identity",
|
||||
columns: ["entity_id", "provider"],
|
||||
unique: true,
|
||||
})
|
||||
|
||||
const authIdentityIndexName = "IDX_provider_identity_auth_identity_id"
|
||||
const authIdentityIndexStatement = createPsqlIndexStatementHelper({
|
||||
name: authIdentityIndexName,
|
||||
tableName: "provider_identity",
|
||||
columns: ["auth_identity_id"],
|
||||
})
|
||||
|
||||
@Entity()
|
||||
@providerEntityIdIndexStatement.MikroORMIndex()
|
||||
@authIdentityIndexStatement.MikroORMIndex()
|
||||
export default class ProviderIdentity {
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id!: string
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
entity_id: string
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
provider: string
|
||||
|
||||
@ManyToOne(() => AuthIdentity, {
|
||||
columnType: "text",
|
||||
fieldName: "auth_identity_id",
|
||||
mapToPk: true,
|
||||
onDelete: "cascade",
|
||||
})
|
||||
auth_identity_id: string
|
||||
|
||||
@ManyToOne(() => AuthIdentity, {
|
||||
persist: false,
|
||||
})
|
||||
auth_identity: AuthIdentity
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
user_metadata: Record<string, unknown> | null
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
provider_metadata: Record<string, unknown> | null = null
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
created_at: Date
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
onUpdate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
updated_at: Date
|
||||
|
||||
@BeforeCreate()
|
||||
@OnInit()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "provid")
|
||||
this.auth_identity_id ??= this.auth_identity?.id ?? null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user