Revamp auth module to support multiple providers linked to a single auth identity (#7521)
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
||||
AuthenticationResponse,
|
||||
AuthenticationInput,
|
||||
AuthIdentityProviderService,
|
||||
AuthIdentityDTO,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
AbstractAuthModuleProvider,
|
||||
@@ -53,12 +54,11 @@ export class EmailPassAuthService extends AbstractAuthModuleProvider {
|
||||
error: "Email should be a string",
|
||||
}
|
||||
}
|
||||
let authIdentity
|
||||
let authIdentity: AuthIdentityDTO | undefined
|
||||
|
||||
try {
|
||||
authIdentity = await authIdentityService.retrieve({
|
||||
entity_id: email,
|
||||
provider: this.provider,
|
||||
})
|
||||
} catch (error) {
|
||||
if (error.type === MedusaError.Types.NOT_FOUND) {
|
||||
@@ -67,14 +67,16 @@ export class EmailPassAuthService extends AbstractAuthModuleProvider {
|
||||
|
||||
const createdAuthIdentity = await authIdentityService.create({
|
||||
entity_id: email,
|
||||
provider: this.provider,
|
||||
provider_metadata: {
|
||||
password: passwordHash.toString("base64"),
|
||||
},
|
||||
})
|
||||
|
||||
const copy = JSON.parse(JSON.stringify(createdAuthIdentity))
|
||||
delete copy.provider_metadata?.password
|
||||
const providerIdentity = copy.provider_identities?.find(
|
||||
(pi) => pi.provider === this.provider
|
||||
)!
|
||||
delete providerIdentity.provider_metadata?.password
|
||||
|
||||
return {
|
||||
success: true,
|
||||
@@ -85,7 +87,10 @@ export class EmailPassAuthService extends AbstractAuthModuleProvider {
|
||||
return { success: false, error: error.message }
|
||||
}
|
||||
|
||||
const passwordHash = authIdentity.provider_metadata?.password
|
||||
const providerIdentity = authIdentity.provider_identities?.find(
|
||||
(pi) => pi.provider === this.provider
|
||||
)!
|
||||
const passwordHash = providerIdentity.provider_metadata?.password
|
||||
|
||||
if (isString(passwordHash)) {
|
||||
const buf = Buffer.from(passwordHash as string, "base64")
|
||||
@@ -93,7 +98,10 @@ export class EmailPassAuthService extends AbstractAuthModuleProvider {
|
||||
|
||||
if (success) {
|
||||
const copy = JSON.parse(JSON.stringify(authIdentity))
|
||||
delete copy.provider_metadata!.password
|
||||
const providerIdentity = copy.provider_identities?.find(
|
||||
(pi) => pi.provider === this.provider
|
||||
)!
|
||||
delete providerIdentity.provider_metadata?.password
|
||||
|
||||
return {
|
||||
success,
|
||||
|
||||
Reference in New Issue
Block a user