-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUsePasskey.jsx
More file actions
78 lines (69 loc) · 2.15 KB
/
Copy pathUsePasskey.jsx
File metadata and controls
78 lines (69 loc) · 2.15 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
import { signIn, WebAuthnSignInProgressStep } from "@junobuild/core";
import { useEffect, useState } from "react";
import { Button } from "../Button.jsx";
import { Progress } from "./Progress.jsx";
import PropTypes from "prop-types";
export const UsePasskey = ({
progress: wizardProgress,
onProgress: wizardOnProgress,
}) => {
const [progress, setProgress] = useState(undefined);
const onProgress = (progress) => wizardOnProgress({ signIn: progress });
useEffect(() => {
if (wizardProgress === undefined) {
setProgress(undefined);
return;
}
setProgress("signIn" in wizardProgress ? wizardProgress.signIn : null);
}, [wizardProgress]);
const doSignIn = async () => {
try {
await signIn({
webauthn: {
options: { onProgress },
},
});
} catch (error) {
wizardOnProgress(undefined);
// IRL the error would be gracefully displayed to the user unless
// it is one to ignore - for example when the user cancel the flow.
throw error;
}
};
return (
<>
{progress === null ? (
<></>
) : progress === undefined ? (
<>
<p className="pt-6">Already got one set-up?</p>
<Button onClick={doSignIn}>Use your passkey</Button>
</>
) : (
<Progress>
{progress?.step ===
WebAuthnSignInProgressStep.RequestingUserCredential && (
<span>Requesting user credential...</span>
)}
{progress?.step ===
WebAuthnSignInProgressStep.FinalizingCredential && (
<span>Finalizing credential...</span>
)}
{progress?.step === WebAuthnSignInProgressStep.Signing && (
<span>Signing request...</span>
)}
{progress?.step === WebAuthnSignInProgressStep.FinalizingSession && (
<span>Finalizing session...</span>
)}
{progress?.step === WebAuthnSignInProgressStep.RetrievingUser && (
<span>Loading user...</span>
)}
</Progress>
)}
</>
);
};
UsePasskey.propTypes = {
progress: PropTypes.object.isRequired,
onProgress: PropTypes.func.isRequired,
};