|
1
|
export const metadata = { |
|
2
|
title: `How to Use Authentication Routes`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this document, you'll learn about the authentication routes and how to use them to create and log-in users, and reset their password. |
|
8
|
|
|
9
|
<Note> |
|
10
|
|
|
11
|
These routes are added by Medusa's HTTP layer, not the Auth Module. |
|
12
|
|
|
13
|
</Note> |
|
14
|
|
|
15
|
## Types of Authentication Flows |
|
16
|
|
|
17
|
### 1. Basic Authentication Flow |
|
18
|
|
|
19
|
This authentication flow doesn't require validation with third-party services. |
|
20
|
|
|
21
|
<Note title="Example"> |
|
22
|
|
|
23
|
[How to register customer in storefront using basic authentication flow](../../../storefront-development/customers/register/page.mdx). |
|
24
|
|
|
25
|
</Note> |
|
26
|
|
|
27
|
The steps are: |
|
28
|
|
|
29
|
 |
|
30
|
|
|
31
|
1. Register the user with the [Register Route](#register-route). |
|
32
|
2. Use the authentication token to create the user with their respective API route. |
|
33
|
- For example, for customers you would use the [Create Customer API route](!api!/store/customers/register-customer). |
|
34
|
- For admin users, you accept an invite using the [Accept Invite API route](!api!/admin/invites/accept-invite) |
|
35
|
3. Authenticate the user with the [Auth Route](#login-route). |
|
36
|
|
|
37
|
After registration, you only use the [Auth Route](#login-route) for subsequent authentication. |
|
38
|
|
|
39
|
<Note> |
|
40
|
|
|
41
|
To handle errors related to existing identities, refer to [this section](#handling-existing-identities). |
|
42
|
|
|
43
|
</Note> |
|
44
|
|
|
45
|
### 2. Third-Party Service Authenticate Flow |
|
46
|
|
|
47
|
This authentication flow authenticates the user with a third-party service, such as Google. |
|
48
|
|
|
49
|
<Note title="Example"> |
|
50
|
|
|
51
|
[How to authenticate customer with a third-party provider in the storefront.](../../../storefront-development/customers/third-party-login/page.mdx). |
|
52
|
|
|
53
|
</Note> |
|
54
|
|
|
55
|
It requires the following steps: |
|
56
|
|
|
57
|
 |
|
58
|
|
|
59
|
1. Authenticate the user with the [Auth Route](#login-route). |
|
60
|
2. The auth route returns a URL to authenticate with third-party service, such as login with Google. The frontend (such as a storefront), when it receives a `location` property in the response, must redirect to the returned location. |
|
61
|
3. Once the authentication with the third-party service finishes, it redirects back to the frontend with a `code` query parameter. So, make sure your third-party service is configured to redirect to your frontend page after successful authentication. |
|
62
|
4. The frontend sends a request to the [Validate Callback Route](#validate-callback-route) passing it the query parameters received from the third-party service, such as the `code` and `state` query parameters. |
|
63
|
5. If the callback validation is successful, the frontend receives the authentication token. |
|
64
|
6. Decode the received token in the frontend using tools like [react-jwt](https://www.npmjs.com/package/react-jwt). |
|
65
|
- If the decoded data has an `actor_id` property, then the user is already registered. So, use this token for subsequent authenticated requests. |
|
66
|
- If not, follow the rest of the steps. |
|
67
|
7. The frontend uses the authentication token to create the user with their respective API route. |
|
68
|
- For example, for customers you would use the [Create Customer API route](!api!/store/customers/register-customer). |
|
69
|
- For admin users, you accept an invite using the [Accept Invite API route](!api!/admin/invites/accept-invite) |
|
70
|
8. The frontend sends a request to the [Refresh Token Route](#refresh-token-route) to retrieve a new token with the user information populated. |
|
71
|
|
|
72
|
|
|
73
|
--- |
|
74
|
|
|
75
|
## Register Route |
|
76
|
|
|
77
|
The Medusa application defines an API route at `/auth/{actor_type}/{provider}/register` that creates an auth identity for an actor type, such as a `customer`. It returns a JWT token that you pass to an API route that creates the user. |
|
78
|
|
|
79
|
```bash |
|
80
|
curl -X POST http://localhost:9000/auth/{actor_type}/{providers}/register |
|
81
|
-H 'Content-Type: application/json' \ |
|
82
|
--data-raw '{ |
|
83
|
"email": "Whitney_Schultz@gmail.com" |
|
84
|
// ... |
|
85
|
}' |
|
86
|
``` |
|
87
|
|
|
88
|
<Note> |
|
89
|
|
|
90
|
This API route is useful for providers like `emailpass` that uses custom logic to authenticate a user. For authentication providers that authenticate with third-party services, such as Google, use the [Auth Route](#login-route) instead. |
|
91
|
|
|
92
|
</Note> |
|
93
|
|
|
94
|
For example, if you're registering a customer, you: |
|
95
|
|
|
96
|
1. Send a request to `/auth/customer/emailpass/register` to retrieve the registration JWT token. |
|
97
|
2. Send a request to the [Create Customer API route](!api!/store/customers/register-customer) to create the customer, passing the [JWT token in the header](!api!/store/authentication). |
|
98
|
|
|
99
|
### Path Parameters |
|
100
|
|
|
101
|
Its path parameters are: |
|
102
|
|
|
103
|
- `{actor_type}`: the actor type of the user you're authenticating. For example, `customer`. |
|
104
|
- `{provider}`: the auth provider to handle the authentication. For example, `emailpass`. |
|
105
|
|
|
106
|
### Request Body Parameters |
|
107
|
|
|
108
|
This route accepts in the request body the data that the specified authentication provider requires to handle authentication. |
|
109
|
|
|
110
|
For example, the EmailPass provider requires an `email` and `password` fields in the request body. |
|
111
|
|
|
112
|
### Response Fields |
|
113
|
|
|
114
|
If the authentication is successful, you'll receive a `token` field in the response body object: |
|
115
|
|
|
116
|
```json |
|
117
|
{ |
|
118
|
"token": "..." |
|
119
|
} |
|
120
|
``` |
|
121
|
|
|
122
|
Use that token in the header of subsequent requests to send authenticated requests. |
|
123
|
|
|
124
|
### Handling Existing Identities |
|
125
|
|
|
126
|
An auth identity with the same email may already exist in Medusa. This can happen if: |
|
127
|
|
|
128
|
- Another actor type is using that email. For example, an admin user is trying to register as a customer. |
|
129
|
- The same email belongs to a record of the same actor type. For example, another customer has the same email. |
|
130
|
|
|
131
|
In these scenarios, the Register Route will return an error instead of a token: |
|
132
|
|
|
133
|
```json |
|
134
|
{ |
|
135
|
"type": "unauthorized", |
|
136
|
"message": "Identity with email already exists" |
|
137
|
} |
|
138
|
``` |
|
139
|
|
|
140
|
To handle these scenarios, you can use the [Login Route](#login-route) to validate that the email and password match the existing identity. If so, you can allow the admin user, for example, to register as a customer. |
|
141
|
|
|
142
|
Otherwise, if the email and password don't match the existing identity, such as when the email belongs to another customer, the [Login Route](#login-route) returns an error: |
|
143
|
|
|
144
|
```json |
|
145
|
{ |
|
146
|
"type": "unauthorized", |
|
147
|
"message": "Invalid email or password" |
|
148
|
} |
|
149
|
``` |
|
150
|
|
|
151
|
You can show that error message to the customer. |
|
152
|
|
|
153
|
--- |
|
154
|
|
|
155
|
## Login Route |
|
156
|
|
|
157
|
The Medusa application defines an API route at `/auth/{actor_type}/{provider}` that authenticates a user of an actor type. It returns a JWT token that can be passed in [the header of subsequent requests](!api!/store/authentication) to send authenticated requests. |
|
158
|
|
|
159
|
```bash |
|
160
|
curl -X POST http://localhost:9000/auth/{actor_type}/{providers} |
|
161
|
-H 'Content-Type: application/json' \ |
|
162
|
--data-raw '{ |
|
163
|
"email": "Whitney_Schultz@gmail.com" |
|
164
|
// ... |
|
165
|
}' |
|
166
|
``` |
|
167
|
|
|
168
|
For example, if you're authenticating a customer, you send a request to `/auth/customer/emailpass`. |
|
169
|
|
|
170
|
### Path Parameters |
|
171
|
|
|
172
|
Its path parameters are: |
|
173
|
|
|
174
|
- `{actor_type}`: the actor type of the user you're authenticating. For example, `customer`. |
|
175
|
- `{provider}`: the auth provider to handle the authentication. For example, `emailpass`. |
|
176
|
|
|
177
|
### Request Body Parameters |
|
178
|
|
|
179
|
This route accepts in the request body the data that the specified authentication provider requires to handle authentication. |
|
180
|
|
|
181
|
For example, the EmailPass provider requires an `email` and `password` fields in the request body. |
|
182
|
|
|
183
|
#### Overriding Callback URL |
|
184
|
|
|
185
|
For the [GitHub](../auth-providers/github/page.mdx) and [Google](../auth-providers/google/page.mdx) providers, you can pass a `callback_url` body parameter that overrides the `callbackUrl` set in the provider's configurations. |
|
186
|
|
|
187
|
This is useful if you want to redirect the user to a different URL after authentication based on their actor type. For example, you can set different `callback_url` for admin users and customers. |
|
188
|
|
|
189
|
### Response Fields |
|
190
|
|
|
191
|
If the authentication is successful, you'll receive a `token` field in the response body object: |
|
192
|
|
|
193
|
```json |
|
194
|
{ |
|
195
|
"token": "..." |
|
196
|
} |
|
197
|
``` |
|
198
|
|
|
199
|
Use that token in the header of subsequent requests to send authenticated requests. |
|
200
|
|
|
201
|
If the authentication requires more action with a third-party service, you'll receive a `location` property: |
|
202
|
|
|
203
|
```json |
|
204
|
{ |
|
205
|
"location": "https://..." |
|
206
|
} |
|
207
|
``` |
|
208
|
|
|
209
|
Redirect to that URL in the frontend to continue the authentication process with the third-party service. |
|
210
|
|
|
211
|
#### Customizing the Response Body |
|
212
|
|
|
213
|
A custom auth provider can't change the shape of this route's response body. The provider's [authenticate method](/references/auth/provider#authenticate) returns an [AuthenticationResponse](/references/auth/interfaces/auth.AuthenticationResponse) object, but this route only uses its `success`, `authIdentity`, `location`, and `error` fields to build the response. Any other fields you return from the provider are ignored. |
|
214
|
|
|
215
|
If you need to return a custom response body, create a [custom API route](!docs!/learn/fundamentals/api-routes) that calls the [Auth Module's authenticate method](/references/auth/authenticate), then shape the response as you need. |
|
216
|
|
|
217
|
<Note title="Example"> |
|
218
|
|
|
219
|
[How to login Customers using the authentication route](../../../storefront-development/customers/login/page.mdx). |
|
220
|
|
|
221
|
</Note> |
|
222
|
|
|
223
|
--- |
|
224
|
|
|
225
|
## Validate Callback Route |
|
226
|
|
|
227
|
The Medusa application defines an API route at `/auth/{actor_type}/{provider}/callback` that's useful for validating the authentication callback or redirect from third-party services like Google. |
|
228
|
|
|
229
|
```bash |
|
230
|
curl -X POST http://localhost:9000/auth/{actor_type}/{providers}/callback?code=123&state=456 |
|
231
|
``` |
|
232
|
|
|
233
|
<Note title="Tip"> |
|
234
|
|
|
235
|
Refer to the [third-party authentication flow](#2-third-party-service-authenticate-flow) section to see how this route fits into the authentication flow. |
|
236
|
|
|
237
|
</Note> |
|
238
|
|
|
239
|
### Path Parameters |
|
240
|
|
|
241
|
Its path parameters are: |
|
242
|
|
|
243
|
- `{actor_type}`: the actor type of the user you're authenticating. For example, `customer`. |
|
244
|
- `{provider}`: the auth provider to handle the authentication. For example, `google`. |
|
245
|
|
|
246
|
### Query Parameters |
|
247
|
|
|
248
|
This route accepts all the query parameters that the third-party service sends to the frontend after the user completes the authentication process, such as the `code` and `state` query parameters. |
|
249
|
|
|
250
|
### Response Fields |
|
251
|
|
|
252
|
If the authentication is successful, you'll receive a `token` field in the response body object: |
|
253
|
|
|
254
|
```json |
|
255
|
{ |
|
256
|
"token": "..." |
|
257
|
} |
|
258
|
``` |
|
259
|
|
|
260
|
In your frontend, decode the token using tools like [react-jwt](https://www.npmjs.com/package/react-jwt): |
|
261
|
|
|
262
|
- If the decoded data has an `actor_id` property, the user is already registered. So, use this token for subsequent authenticated requests. |
|
263
|
- If not, use the token in the header of a request that creates the user, such as the [Create Customer API route](!api!/store/customers/register-customer). |
|
264
|
|
|
265
|
The decoded data may look like this: |
|
266
|
|
|
267
|
```json |
|
268
|
{ |
|
269
|
"actor_id": "", // Empty if the user is not registered |
|
270
|
"user_metadata": { |
|
271
|
"email": "Whitney_Schultz@gmail.com" |
|
272
|
} |
|
273
|
// other fields... |
|
274
|
} |
|
275
|
``` |
|
276
|
|
|
277
|
--- |
|
278
|
|
|
279
|
## Refresh Token Route |
|
280
|
|
|
281
|
The Medusa application defines an API route at `/auth/token/refresh` that's useful after authenticating a user with a third-party service to populate the user's token with their new information. |
|
282
|
|
|
283
|
It requires the user's JWT token that they received from the authentication or callback routes. |
|
284
|
|
|
285
|
```bash |
|
286
|
curl -X POST http://localhost:9000/auth/token/refresh \ |
|
287
|
-H 'Authorization: Bearer {token}' |
|
288
|
``` |
|
289
|
|
|
290
|
### Response Fields |
|
291
|
|
|
292
|
If the token was refreshed successfully, you'll receive a `token` field in the response body object: |
|
293
|
|
|
294
|
```json |
|
295
|
{ |
|
296
|
"token": "..." |
|
297
|
} |
|
298
|
``` |
|
299
|
|
|
300
|
Use that token in the header of subsequent requests to send authenticated requests. |
|
301
|
|
|
302
|
--- |
|
303
|
|
|
304
|
## Reset Password Routes |
|
305
|
|
|
306
|
To reset a user's password: |
|
307
|
|
|
308
|
1. Generate a token using the [Generate Reset Password Token API route](#generate-reset-password-token-route). |
|
309
|
- The API route emits the `auth.password_reset` event, passing the token in the payload. |
|
310
|
- You can create a subscriber, as seen in [this guide](../reset-password/page.mdx), that listens to the event and send a notification to the user. |
|
311
|
2. Pass the token to the [Reset Password API route](#reset-password-route) to reset the password. |
|
312
|
- The URL in the user's notification should direct them to a frontend URL, which sends a request to this route. |
|
313
|
|
|
314
|
<Note title="Example"> |
|
315
|
|
|
316
|
[Storefront Development: How to Reset a Customer's Password.](../../../storefront-development/customers/reset-password/page.mdx) |
|
317
|
|
|
318
|
</Note> |
|
319
|
|
|
320
|
### Generate Reset Password Token Route |
|
321
|
|
|
322
|
<Note> |
|
323
|
|
|
324
|
This route is available since [Medusa v2.16.0](https://github.com/medusajs/medusa/releases/tag/v2.16.0). On earlier versions, the route does not exist. |
|
325
|
|
|
326
|
</Note> |
|
327
|
|
|
328
|
The Medusa application defines an API route at `/auth/{actor_type}/{auth_provider}/reset-password` that emits the `auth.password_reset` event, passing the token in the payload. |
|
329
|
|
|
330
|
```bash |
|
331
|
curl -X POST http://localhost:9000/auth/{actor_type}/{providers}/reset-password |
|
332
|
-H 'Content-Type: application/json' \ |
|
333
|
--data-raw '{ |
|
334
|
"identifier": "Whitney_Schultz@gmail.com" |
|
335
|
}' |
|
336
|
``` |
|
337
|
|
|
338
|
<Note> |
|
339
|
|
|
340
|
This API route is useful for providers like `emailpass` that store a user's password and use it for authentication. |
|
341
|
|
|
342
|
</Note> |
|
343
|
|
|
344
|
#### Path Parameters |
|
345
|
|
|
346
|
Its path parameters are: |
|
347
|
|
|
348
|
- `{actor_type}`: the actor type of the user you're authenticating. For example, `customer`. |
|
349
|
- `{provider}`: the auth provider to handle the authentication. For example, `emailpass`. |
|
350
|
|
|
351
|
#### Request Body Parameters |
|
352
|
|
|
353
|
This route accepts in the request body an object having the following property: |
|
354
|
|
|
355
|
- `identifier`: The user's identifier in the specified auth provider. For example, for the `emailpass` auth provider, you pass the user's email. |
|
356
|
|
|
357
|
#### Response Fields |
|
358
|
|
|
359
|
If the authentication is successful, the request returns a `201` response code. |
|
360
|
|
|
361
|
### Reset Password Route |
|
362
|
|
|
363
|
The Medusa application defines an API route at `/auth/{actor_type}/{auth_provider}/update` that accepts a token and, if valid, updates the user's password. |
|
364
|
|
|
365
|
```bash |
|
366
|
curl -X POST http://localhost:9000/auth/{actor_type}/{providers}/update |
|
367
|
-H 'Content-Type: application/json' \ |
|
368
|
-H 'Authorization: Bearer {token}' \ |
|
369
|
--data-raw '{ |
|
370
|
"email": "Whitney_Schultz@gmail.com", |
|
371
|
"password": "supersecret" |
|
372
|
}' |
|
373
|
``` |
|
374
|
|
|
375
|
<Note> |
|
376
|
|
|
377
|
This API route is useful for providers like `emailpass` that store a user's password and use it for logging them in. |
|
378
|
|
|
379
|
</Note> |
|
380
|
|
|
381
|
#### Path Parameters |
|
382
|
|
|
383
|
Its path parameters are: |
|
384
|
|
|
385
|
- `{actor_type}`: the actor type of the user you're authenticating. For example, `customer`. |
|
386
|
- `{provider}`: the auth provider to handle the authentication. For example, `emailpass`. |
|
387
|
|
|
388
|
#### Pass Token in Authorization Header |
|
389
|
|
|
390
|
<Note> |
|
391
|
|
|
392
|
Before [Medusa v2.6](https://github.com/medusajs/medusa/releases/tag/v2.6), you passed the token as a query parameter. Now, you must pass it in the `Authorization` header. |
|
393
|
|
|
394
|
</Note> |
|
395
|
|
|
396
|
In the request's authorization header, you must pass the token generated using the [Generate Reset Password Token route](#generate-reset-password-token-route). You pass it as a bearer token. |
|
397
|
|
|
398
|
### Request Body Parameters |
|
399
|
|
|
400
|
This route accepts in the request body an object that has the data necessary for the provider to update the user's password. |
|
401
|
|
|
402
|
For the `emailpass` provider, you must pass the following properties: |
|
403
|
|
|
404
|
- `email`: The user's email. |
|
405
|
- `password`: The new password. |
|
406
|
|
|
407
|
### Response Fields |
|
408
|
|
|
409
|
If the authentication is successful, the request returns an object with a `success` property set to `true`: |
|
410
|
|
|
411
|
```json |
|
412
|
{ |
|
413
|
"success": "true" |
|
414
|
} |
|
415
|
``` |