1 export const metadata = {
2 title: `Customer Accounts`,
3 }
4
5 # {metadata.title}
6
7 In this document, you’ll learn how registered and unregistered accounts are distinguished in the Medusa application.
8
9 <Note title="Looking for no-code docs?">
10
11 Refer to this [Medusa Admin User Guide](!user-guide!/customers) to learn how to manage customers using the dashboard.
12
13 </Note>
14
15 ## `has_account` Property
16
17 The [Customer data model](/references/customer/models/Customer) has a `has_account` property, which is a boolean that indicates whether a customer is registered.
18
19 When a guest customer places an order, a new `Customer` record is created with `has_account` set to `false`.
20
21 When this or another guest customer registers an account with the same email, a new `Customer` record is created with `has_account` set to `true`.
22
23 ---
24
25 ## Email Uniqueness
26
27 The above behavior means that two `Customer` records may exist with the same email address. However, the main difference is the `has_account` property's value.
28
29 So, there can only be one guest customer (having `has_account=false`) and one registered customer (having `has_account=true`) with the same email address.
30
31 ---
32
33 ## Change a Guest Customer to a Registered Customer
34
35 You can only set the `has_account` property when you create a customer. The [updateCustomersWorkflow](/references/medusa-workflows/updateCustomersWorkflow) and the Customer Module's `updateCustomers` method don't accept it. So, you can't change an existing guest customer to a registered customer.
36
37 This affects flows that link a new auth identity to an existing guest customer. For example, a custom auth provider that sets the `customer_id` in the auth identity's app metadata to the guest customer's ID. The customer can log in, but the Medusa Admin still shows them as a guest.
38
39 Instead, use one of the following approaches.
40
41 ### Set `has_account` When Importing Customers
42
43 If you're migrating customers from another platform, set `has_account` to `true` for customers that have accounts when you create them. How you do that depends on whether the customer's auth identity already exists at import time.
44
45 #### Auth Identity Exists
46
47 If you create the customer's auth identity before importing the customer, use the [createCustomerAccountWorkflow](/references/medusa-workflows/createCustomerAccountWorkflow). It creates the customer with `has_account` set to `true` and sets the customer's ID in the auth identity's app metadata.
48
49 For example, in a [custom CLI script](!docs!/learn/fundamentals/custom-cli-scripts):
50
51 ```ts title="src/scripts/import-customers.ts"
52 import { ExecArgs } from "@medusajs/framework/types"
53 import {
54 createCustomerAccountWorkflow,
55 } from "@medusajs/medusa/core-flows"
56
57 export default async function importCustomers({
58 container,
59 }: ExecArgs) {
60 await createCustomerAccountWorkflow(container).run({
61 input: {
62 authIdentityId: "authid_123",
63 customerData: {
64 email: "john@example.com",
65 },
66 },
67 })
68 }
69 ```
70
71 The workflow creates one customer per run, so run it for each customer you import.
72
73 #### Auth Identity Is Created on First Login
74
75 Some auth providers, such as passwordless providers, create the auth identity when the customer first logs in. In that case, create the customers with `has_account` set to `true` using the [createCustomersWorkflow](/references/medusa-workflows/createCustomersWorkflow). It accepts multiple customers in one run:
76
77 export const importHighlights = [
78 ["14", "has_account", "Mark the customer as registered."]
79 ]
80
81 ```ts title="src/scripts/import-customers.ts" highlights={importHighlights}
82 import { ExecArgs } from "@medusajs/framework/types"
83 import {
84 createCustomersWorkflow,
85 } from "@medusajs/medusa/core-flows"
86
87 export default async function importCustomers({
88 container,
89 }: ExecArgs) {
90 await createCustomersWorkflow(container).run({
91 input: {
92 customersData: [
93 {
94 email: "john@example.com",
95 has_account: true,
96 },
97 ],
98 },
99 })
100 }
101 ```
102
103 Then, when the customer first logs in, link the new auth identity to the existing customer. Create a workflow that uses the [setAuthAppMetadataStep](/references/medusa-workflows/steps/setAuthAppMetadataStep) to set the customer's ID in the auth identity's app metadata:
104
105 export const linkHighlights = [
106 ["19", "actorType", "Sets the `customer_id` key in the app metadata."],
107 ["20", "value", "The ID of the imported customer."]
108 ]
109
110 ```ts title="src/workflows/link-customer-auth-identity.ts" highlights={linkHighlights}
111 import {
112 createWorkflow,
113 WorkflowResponse,
114 } from "@medusajs/framework/workflows-sdk"
115 import {
116 setAuthAppMetadataStep,
117 } from "@medusajs/medusa/core-flows"
118
119 type LinkCustomerAuthIdentityInput = {
120 authIdentityId: string
121 customerId: string
122 }
123
124 export const linkCustomerAuthIdentityWorkflow = createWorkflow(
125 "link-customer-auth-identity",
126 (input: LinkCustomerAuthIdentityInput) => {
127 const authIdentity = setAuthAppMetadataStep({
128 authIdentityId: input.authIdentityId,
129 actorType: "customer",
130 value: input.customerId,
131 })
132
133 return new WorkflowResponse(authIdentity)
134 }
135 )
136 ```
137
138 Execute the workflow in your login flow after the auth provider creates the auth identity. The step throws an error if the auth identity is already linked to a customer.
139
140 ### Register a New Customer Account
141
142 When a guest customer registers, use the [createCustomerAccountWorkflow](/references/medusa-workflows/createCustomerAccountWorkflow) to create a registered customer with the same email. The workflow also sets the customer's ID in the auth identity's app metadata. It doesn't change the existing guest customer.
143
144 The guest customer's orders stay linked to the guest customer. To move them to the registered customer, the customer can request an order transfer using the [Request Order Transfer API route](!api!/store#orders_postordersidtransferrequest).
145
146 ---
147
148 ## Customer Deletion and Email Reuse
149
150 When a merchant deletes a customer, the `Customer` record is soft-deleted, meaning it is not permanently removed from the database.
151
152 When using the Medusa Application with the [Auth Module](../../auth/page.mdx), possible confusion may arise in the following scenarios:
153
154 1. An admin user is using the email address `john@example.com`, and a customer tries to register with the same email address.
155 2. An admin user has deleted a customer with the email address `jane@example.com`, and another customer tries to register with the same email address.
156
157 In these and similar scenarios, the customer trying to register will receive an error message indicating that the email address is already in use:
158
159 ```json
160 {
161 "type": "unauthorized",
162 "message": "Identity with email already exists"
163 }
164 ```
165
166 To resolve this, you can amend the registration flow to:
167
168 1. Retrieve the login token of the existing identity with the same email address.
169 2. Use the login token when registering the new customer. This will not remove the existing identity but will allow the new customer to register with the same email address.
170
171 You can learn more about how to implement this flow in the following guides:
172
173 - [Conceptual guide on how to implement this flow with Medusa's authentication routes](../../auth/authentication-route/page.mdx#handling-existing-identities).
174 - [How-to guide on how to implement this in a storefront](../../../storefront-development/customers/register/page.mdx).