-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.ts
More file actions
190 lines (159 loc) · 5.4 KB
/
main.ts
File metadata and controls
190 lines (159 loc) · 5.4 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (c) 2025 Ping Identity Corporation. All rights reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
import './style.css';
import { journey } from '@forgerock/journey-client';
import type {
NameCallback,
PasswordCallback,
RequestMiddleware,
} from '@forgerock/journey-client/types';
import passwordComponent from './components/password.js';
import textComponent from './components/text.js';
import { serverConfigs } from './server-configs.js';
import { webauthnComponent } from './components/webauthn.js';
import { WebAuthn, WebAuthnStepType } from '@forgerock/journey-client/webauthn';
const qs = window.location.search;
const searchParams = new URLSearchParams(qs);
const journeyName = searchParams.get('clientId') || 'UsernamePassword';
const config = serverConfigs[journeyName];
const requestMiddleware: RequestMiddleware[] = [
(req, action, next) => {
switch (action.type) {
case 'JOURNEY_START':
if ((action.payload as any).type === 'service') {
console.log('Starting authentication with service');
}
break;
case 'JOURNEY_NEXT':
if (!('type' in (action.payload as any))) {
console.log('Continuing authentication with service');
}
break;
}
next();
},
];
(async () => {
const journeyClient = await journey({ config, requestMiddleware });
const errorEl = document.getElementById('error') as HTMLDivElement;
const formEl = document.getElementById('form') as HTMLFormElement;
const journeyEl = document.getElementById('journey') as HTMLDivElement;
let step = await journeyClient.start({ ...config, journey: journeyName });
function renderComplete() {
if (step?.type !== 'LoginSuccess') {
throw new Error('Expected step to be defined and of type LoginSuccess');
}
const session = step.getSessionToken();
journeyEl.innerHTML = `
<h2>Complete</h2>
<span>Session:</span>
<pre data-testid="sessionToken" id="sessionToken">${session}</pre>
<button type="button" id="logoutButton">Logout</button>
`;
const loginBtn = document.getElementById('logoutButton') as HTMLButtonElement;
loginBtn.addEventListener('click', async () => {
await journeyClient.terminate();
console.log('Logout successful');
step = await journeyClient.start();
renderForm();
});
}
function renderError() {
if (step?.type !== 'LoginFailure') {
throw new Error('Expected step to be defined and of type LoginFailure');
}
const error = step.payload.message;
if (errorEl) {
errorEl.innerHTML = `
<pre>${error}</pre>
`;
}
}
// Represents the main render function for app
async function renderForm() {
journeyEl.innerHTML = '';
if (step?.type !== 'Step') {
throw new Error('Expected step to be defined and of type Step');
}
const formName = step.getHeader();
const header = document.createElement('h2');
header.innerText = formName || '';
journeyEl.appendChild(header);
const webAuthnStep = WebAuthn.getWebAuthnStepType(step);
if (
webAuthnStep === WebAuthnStepType.Authentication ||
webAuthnStep === WebAuthnStepType.Registration
) {
await webauthnComponent(journeyEl, step, 0);
step = await journeyClient.next(step);
if (step?.type === 'Step') {
await renderForm();
} else if (step?.type === 'LoginSuccess') {
console.log('Basic login successful');
renderComplete();
} else if (step?.type === 'LoginFailure') {
renderForm();
renderError();
} else {
console.error('Unknown node status', step);
}
return; // prevent the rest of the function from running
}
const callbacks = step.callbacks;
callbacks.forEach(async (callback, idx) => {
if (callback.getType() === 'NameCallback') {
const cb = callback as NameCallback;
textComponent(
journeyEl, // You can ignore this; it's just for rendering
cb, // This callback class
idx,
);
} else if (callback.getType() === 'PasswordCallback') {
const cb = callback as PasswordCallback;
passwordComponent(
journeyEl, // You can ignore this; it's just for rendering
cb, // This callback class
idx,
);
}
});
const submitBtn = document.createElement('button');
submitBtn.type = 'submit';
submitBtn.id = 'submitButton';
submitBtn.innerText = 'Submit';
journeyEl.appendChild(submitBtn);
}
formEl.addEventListener('submit', async (event) => {
event.preventDefault();
if (step?.type !== 'Step') {
throw new Error('Expected step to be defined and of type Step');
}
/**
* We can just call `next` here and not worry about passing any arguments
*/
step = await journeyClient.next(step);
/**
* Recursively render the form with the new state
*/
if (step?.type === 'Step') {
await renderForm();
} else if (step?.type === 'LoginSuccess') {
console.log('Basic login successful');
renderComplete();
} else if (step?.type === 'LoginFailure') {
renderForm();
renderError();
} else {
console.error('Unknown node status', step);
}
});
if (step?.type !== 'LoginSuccess') {
renderForm();
} else {
renderComplete();
}
})();