-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathindex.ts
More file actions
64 lines (58 loc) · 2.34 KB
/
index.ts
File metadata and controls
64 lines (58 loc) · 2.34 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
import { handleLoginFlowResult } from "$src/components/authenticateBox";
import {
WEBAUTHN_IFRAME_PATH,
webAuthnInIframeFlow,
} from "$src/flows/iframeWebAuthn";
import { nonNullish } from "@dfinity/utils";
import { registerTentativeDevice } from "./flows/addDevice/welcomeView/registerTentativeDevice";
import { authFlowAuthorize } from "./flows/authorize";
import { authFlowManage, renderManageWarmup } from "./flows/manage";
import { createSpa } from "./spa";
import { getAddDeviceAnchor } from "./utils/addDeviceLink";
import { analytics, initAnalytics } from "./utils/analyitcs/analytics";
void createSpa(async (connection) => {
initAnalytics(connection.canisterConfig.analytics_config[0]?.[0]);
analytics.pageView();
// Figure out if user is trying to add a device. If so, use the anchor from the URL.
const addDeviceAnchor = getAddDeviceAnchor();
if (nonNullish(addDeviceAnchor)) {
analytics.event("page-add-new-device");
const userNumber = addDeviceAnchor;
// Register this device (tentatively)
const registerDeviceResult = await registerTentativeDevice(
addDeviceAnchor,
connection,
);
if (registerDeviceResult.tag === "canceled") {
// Adding a device was canceled, fall back into default flow
return authFlowManage(connection);
}
registerDeviceResult satisfies { tag: "deviceAdded" };
const renderManage = renderManageWarmup();
// If user "Click" continue in success page, proceed with authentication
const result = await connection.login(userNumber);
const loginData = await handleLoginFlowResult(result);
// User have successfully signed-in we can jump to manage page
if (nonNullish(loginData)) {
return await renderManage({
userNumber,
connection: loginData.connection,
});
}
}
const url = new URL(document.URL);
// Simple, #-based routing
if (url.hash === "#authorize") {
analytics.event("page-authorize");
// User was brought here by a dapp for authorization
return authFlowAuthorize(connection);
} else if (url.hash === WEBAUTHN_IFRAME_PATH) {
analytics.event("page-webauthn-iframe");
// User needs to do cross-origin WebAuthn authentication in an iframe
return webAuthnInIframeFlow(connection);
} else {
analytics.event("page-manage");
// The default flow
return authFlowManage(connection);
}
});