Feature Request: Native Support for "Identity Merging" (Auto-Link by Email) #14350
mocker12345
started this conversation in
Feature Requests
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem Description
In the current Medusa v2 architecture, utilizing multiple authentication providers (e.g.,
google,emailpass,phone) often creates a fragmented customer experience.If a user logs in with
[email protected]via Google, and later logs in with[email protected]via Email/Password (or OTP), Medusa currently treats these as two separate identities. Without custom logic, they may end up creating two distinctCustomerrecords (if the unique email constraint doesn't block it) or failing to link the new identity to the existing customer.Developers currently have to "shadow" (overwrite) the
POST /store/customersroute or write custom Callback Routes to manually query for existing customers and establish Remote Links.Proposed Solution
We propose adding an optional configuration to the Auth/Customer modules (e.g.,
auto_merge_identities: true) or a standard "Linking Workflow" that provides this behavior out-of-the-box.Logic Flow
AuthIdentityis linked to aCustomer.Customertable for a matchingemail.AuthIdentityto the existingCustomer.Customer.Why is this important?
Our Current Workaround
We achieved this by:
AuthandCustomer.defineMiddlewaresto interceptPOST /store/customers.auth_contextis present (Authenticated), we run our custom "Link Workflow" and return early.next()to let the core logic handle standard creation.find-and-link-stepthat performs the lookup and merge logic.Technical Discovery: "isList: true" Requirement
If implementing this natively, please note a critical configuration detail we discovered:
When defining the link between
AuthIdentityandCustomer, thematchMUST allow many-to-one relationships.By default,
defineLinkseems to enforce One-to-One. We had to explicitly setisList: true:Without this, attempts to link a second identity fail with:
Cannot create multiple links between 'auth' and 'customer'.Reference Implementation
Here is a simplified version of the logic we implemented in our
FindAndLinkworkflow step:It would be great to see this pattern adopted or supported natively in the framework!
Considerations: No-Email Providers
We recognize that not all providers return an email (e.g., Phone Auth, some Twitter logins).
For the native implementation, we suggest:
{provider_id}@no-email.com) to satisfy the unique constraint, or make the email field optional in the core.Beta Was this translation helpful? Give feedback.
All reactions