-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrationsteps node.js
88 lines (65 loc) · 2.78 KB
/
integrationsteps node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
### Your app displays the sign-in page
When the user launches the app, it will display the sign-in page.
Build a sign-in page that captures their username and password.
<div class="half wireframe-border">

<!--
Source image: https://www.figma.com/file/YH5Zhzp66kGCglrXQUag2E/%F0%9F%93%8A-Updated-Diagrams-for-Dev-Docs?node-id=3398%3A36678&t=wzNwSZkdctajVush-1 sign-in-form-username-password
-->
</div>
### The user submits their username and password
When the user submits their `username` and `password`, pass them as parameters to [`OktaAuth.idx.authenticate()`](https://github.com/okta/okta-auth-js/blob/master/docs/idx.md#idxauthenticate) to capture their login credentials.
```javascript
const authClient = getAuthClient(req);
const transaction = await authClient.idx.authenticate({ username, password });
```
### Your app processes the authentication response
`authenticate()` returns a `transaction` object with a `status` property indicating the status of the sign-in flow. The returned `IdxStatus` value displays the status of the sign-in flow. Handle the returned values accordingly:
#### Processing successful login
After the user supplies their correct password, `IdxStatus` equals `IdxStatus.SUCCESS`. Call `tokenManager.setTokens()` to save the tokens retrieved for future requests and redirect the user back to the home page. The user is now signed in.
```js
const { nextStep, tokens, status, error, } = transaction;
// Persist states to session
req.setFlowStates({ idx: transaction });
switch (status) {
case IdxStatus.SUCCESS:
// Save tokens to storage (req.session)
authClient.tokenManager.setTokens(tokens);
// Redirect back to home page
res.redirect('/');
return;
// Handle other statuses
}
```
#### Handling other authentication statuses
The app must handle other `IdxStatus` values if the user didn't successfully sign-in or if additional validation is required.
See the below process flow for each returned `IdxStatus` value captured in the relevant case statements:
```js
switch (status) {
case IdxStatus.SUCCESS:
// handle success
return;
case IdxStatus.PENDING:
// Proceed to next step
try {
if (!proceed({ req, res, nextStep })) {
next(new Error(`
Oops! The current flow cannot support the policy configuration in your org.
`));
}
} catch (err) {
next(err);
}
return;
case IdxStatus.FAILURE:
authClient.transactionManager.clear();
next(error);
return;
case IdxStatus.TERMINAL:
redirect({ req, res, path: '/terminal' });
return;
case IdxStatus.CANCELED:
res.redirect('/');
return;
}
```