Skip to content

Commit 9541bd7

Browse files
committed
Add graphql and sdk coding samples
1 parent 13f0768 commit 9541bd7

File tree

1 file changed

+178
-7
lines changed

1 file changed

+178
-7
lines changed

content/4.auth/5.email-login.md

Lines changed: 178 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,41 @@ Read more about creating users.
2222

2323
You can authenticate as a user to receive a standard token.
2424

25-
```json [POST /auth/login]
26-
{
27-
"email": "[email protected]",
28-
"password": "d1r3ctu5"
25+
## Logging In
26+
27+
::snippets
28+
#rest
29+
```bash [Terminal]
30+
curl \
31+
--request POST \
32+
--header 'Content-Type: application/json' \
33+
--data '{ "email": "[email protected]", "password": "d1r3ctu5" }' \
34+
--url 'https://directus.example.com/auth/login'
35+
```
36+
37+
#graphql
38+
```graphql
39+
mutation {
40+
auth_login(email: "[email protected]", password: "d1r3ctu5") {
41+
access_token
42+
refresh_token
43+
}
2944
}
3045
```
3146

47+
#sdk
48+
```js
49+
import { createDirectus, authentication } from '@directus/sdk';
50+
51+
const email = "[email protected]";
52+
const password = "d1r3ctu5";
53+
54+
const client = createDirectus('http://directus.example.com').with(authentication());
55+
56+
const token = await client.login(email, password);
57+
```
58+
::
59+
3260
If the user has [two-factor authentication](/auth/2fa) enabled, an `otp` (one-time password) can be passed as an additional property. The response will contain a standard token.
3361

3462
:partial{content="snippet-auth-token"}
@@ -37,56 +65,176 @@ If the user has [two-factor authentication](/auth/2fa) enabled, an `otp` (one-ti
3765

3866
<!-- TODO: Clarify the different modes -->
3967

40-
If you wish to receive and store a session cookie, add a `mode` property when logging in. The token won't be returned in JSON response.
68+
If you wish to receive and store a session cookie, add a `mode` property when logging in.
4169

70+
::snippets
71+
#rest
4272
```json [POST /auth/login]
4373
{
4474
"email": "[email protected]",
45-
"password": "d1r3ctu5",
46-
"mode": "session"
75+
"password": "d1r3ctu5"
4776
}
4877
```
78+
The token won't be returned in JSON response.
79+
80+
#graphql
81+
```graphql
82+
mutation {
83+
auth_login(email: "[email protected]", password: "d1r3ctu5", mode: "session") {
84+
access_token
85+
refresh_token
86+
}
87+
}
88+
```
89+
90+
#sdk
91+
```js
92+
import { createDirectus, authentication } from '@directus/sdk';
93+
94+
const email = "[email protected]";
95+
const password = "d1r3ctu5";
96+
97+
const client = createDirectus('http://directus.example.com').with(authentication());
98+
99+
const token = await client.login(email, password, {mode: "session"});
100+
```
101+
::
49102

50103
## Refresh
51104

52105
Retrieve a new access token by refreshing it.
53106

107+
::snippets
108+
#rest
54109
```json [POST /auth/refresh]
55110
{
56111
"refresh_token": "Xp2tTNAdLYfnaAOOjt3oetyCWtobKKUIeEXj..."
57112
}
58113
```
59114

115+
#graphql
116+
117+
```graphql
118+
mutation {
119+
auth_refresh(refresh_token: "refresh_token") {
120+
access_token
121+
refresh_token
122+
}
123+
}
124+
```
125+
126+
# sdk
127+
128+
```js
129+
import { createDirectus, authentication, rest, refresh } from '@directus/sdk';
130+
131+
const client = createDirectus('directus_project_url').with(authentication()).with(rest());
132+
133+
// refresh http request using json
134+
const result = await client.request(refresh('json', refresh_token));
135+
```
136+
60137
### Refreshing a Cookie
61138

62139
You do not need to provide the `refresh_token`, but you must specify the `mode`.
63140

141+
::snippets
142+
#rest
64143
```json [POST /auth/refresh]
65144
{
66145
"mode": "session"
67146
}
68147
```
69148

149+
#graphql
150+
```graphql
151+
mutation {
152+
auth_refresh(refresh_token: "refresh_token") {
153+
access_token
154+
refresh_token
155+
}
156+
}
157+
```
158+
159+
#sdk
160+
```js
161+
import { createDirectus, authentication, rest, refresh } from '@directus/sdk';
162+
163+
const client = createDirectus('directus_project_url').with(authentication()).with(rest());
164+
165+
// refresh http request using a cookie
166+
const result = await client.request(refresh('cookie'));
167+
```
168+
::
169+
70170
## Logout
71171

72172
Invalidate the refresh token and destroy the user's session.
73173

174+
::snippets
175+
#rest
74176
```json [POST /auth/logout]
75177
{
76178
"refresh_token": "Xp2tTNAdLYfnaAOOjt3oetyCWtobKKUIeEXj..."
77179
}
78180
```
79181

182+
#graphql
183+
```graphql
184+
mutation {
185+
auth_logout(refresh_token: "Xp2tTNAdLYfnaAOOjt3oetyCWtobKKUIeEXj...")
186+
}
187+
```
188+
189+
#sdk
190+
```js
191+
import { createDirectus, authentication, rest, logout } from '@directus/sdk';
192+
193+
const client = createDirectus('directus_project_url').with(authentication()).with(rest());
194+
195+
const result = await client.logout();
196+
```
197+
198+
You can also log out using the http request mechanism:
199+
200+
```js
201+
import { createDirectus, authentication, rest, logout } from '@directus/sdk';
202+
203+
const client = createDirectus('directus_project_url').with(authentication()).with(rest());
204+
205+
const result = await client.request(logout(refresh_token));
206+
```
207+
::
208+
80209
### Invalidating a Cookie
81210

82211
You do not need to provide the `refresh_token`, but you must specify the `mode`. This will immediately invalidate and delete the cookie.
83212

213+
::snippets
214+
#rest
84215
```json [POST /auth/logout]
85216
{
86217
"mode": "session"
87218
}
88219
```
89220

221+
#graphql
222+
```graphql
223+
mutation {
224+
auth_logout(mode: "session")
225+
}
226+
```
227+
228+
#sdk
229+
```js
230+
import { createDirectus, authentication, rest, logout } from '@directus/sdk';
231+
232+
const client = createDirectus('directus_project_url').with(authentication()).with(rest());
233+
234+
const result = await client.logout({mode: "session"});
235+
```
236+
::
237+
90238
## Password Reset
91239

92240
Requesting a password reset will send an email to the user with a URL to the Data Studio to reset their password.
@@ -111,13 +259,36 @@ When using the request reset password endpoint, add a `reset_url` property. The
111259

112260
Your application must extract this value, collect the new user's password, and send both to the reset password endpoint.
113261

262+
::snippets
263+
#rest
114264
```json [POST /auth/password/reset]
115265
{
116266
"token": "Xp2tTNAdLYfnaAOOjt3oetyCWtobKKUIeEXj",
117267
"password": "d1r3ctu5!"
118268
}
119269
```
120270

271+
#graphql
272+
273+
```graphql
274+
mutation {
275+
auth_password_reset(token: "Xp2tTNAdLYfnaAOOjt3oetyCWtobKKUIeEXj", password: "d1r3ctu5!")
276+
}
277+
```
278+
279+
#sdk
280+
281+
```js
282+
import { createDirectus, rest, passwordReset } from '@directus/sdk';
283+
284+
const client = createDirectus('directus_project_url').with(rest());
285+
286+
const reset_token = "Xp2tTNAdLYfnaAOOjt3oetyCWtobKKUIeEXj";
287+
const new_password = "d1r3ctu5!";
288+
289+
const result = await client.request(passwordReset(reset_token, new_password));
290+
```
291+
121292
::callout{type="dev-docs" url="/configuration/security-limits"}
122293
The `PASSWORD_RESET_URL_ALLOW_LIST` environment variable must be configured.
123294
::

0 commit comments

Comments
 (0)