Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion templates/react-example/src/components/Auth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from "prop-types";
import { createContext, useEffect, useState } from "react";
import { Login } from "./Login";
import { Logout } from "./Logout";
import { Passkey } from "./passkey/Passkey";

export const AuthContext = createContext();

Expand All @@ -24,7 +25,10 @@ export const Auth = ({ children }) => {
<Logout />
</div>
) : (
<Login />
<div className="gap flex flex-col">
<Passkey />
<Login />
</div>
)}
</AuthContext.Provider>
);
Expand Down
120 changes: 120 additions & 0 deletions templates/react-example/src/components/passkey/CreatePasskey.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { WebAuthnSignUpProgressStep, signUp } 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 CreatePasskey = ({
progress: wizardProgress,
onProgress: wizardOnProgress,
}) => {
const [progress, setProgress] = useState({ state: "init" });
const [inputText, setInputText] = useState("");

const onProgress = (progress) => wizardOnProgress({ signUp: progress });

useEffect(() => {
if (wizardProgress === undefined) {
setProgress({ state: "init" });
return;
}

setProgress(
"signUp" in wizardProgress
? { state: "progress", detail: wizardProgress.signUp }
: "setup" in wizardProgress
? { state: "setup" }
: { state: "hidden" },
);
}, [wizardProgress]);

const goToSetup = () => {
wizardOnProgress({ setup: null });
};

const doSignUp = async () => {
try {
await signUp({
webauthn: {
options: {
onProgress,
...(inputText !== "" && {
passkey: {
user: {
displayName: inputText,
},
},
}),
},
},
});
} 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.state === "init" ? (
<>
<p>
First time here? Use your device (Face ID, Windows Hello, or screen
lock) to get in.
</p>

<Button onClick={goToSetup}>Create a new passkey</Button>
</>
) : progress.state === "setup" ? (
<>
<p>Want to give it a nickname so you&apos;ll spot it easily later?</p>

<input
className="mx-0 mt-2 mb-6 block w-full resize-none rounded-sm border-[3px] border-black bg-white px-3 py-1.5 text-base font-normal shadow-[5px_5px_0px_rgba(0,0,0,1)] focus:outline-hidden"
placeholder="An optional nickname"
onChange={(e) => {
setInputText(e.target.value);
}}
value={inputText}
></input>

<Button onClick={doSignUp}>Create now</Button>
</>
) : progress.state === "progress" ? (
<Progress>
{progress?.detail.step ===
WebAuthnSignUpProgressStep.CreatingUserCredential && (
<span>Creating user credential...</span>
)}
{progress?.detail.step ===
WebAuthnSignUpProgressStep.ValidatingUserCredential && (
<span>Validating user credential...</span>
)}
{progress?.detail.step ===
WebAuthnSignUpProgressStep.FinalizingCredential && (
<span>Finalizing credential...</span>
)}
{progress?.detail.step === WebAuthnSignUpProgressStep.Signing && (
<span>Signing request....</span>
)}
{progress?.detail.step ===
WebAuthnSignUpProgressStep.FinalizingSession && (
<span>Finalizing session...</span>
)}
{progress?.detail.step ===
WebAuthnSignUpProgressStep.RegisteringUser && (
<span>Registering user...</span>
)}
</Progress>
) : null}
</>
);
};

CreatePasskey.propTypes = {
progress: PropTypes.object.isRequired,
onProgress: PropTypes.func.isRequired,
};
95 changes: 95 additions & 0 deletions templates/react-example/src/components/passkey/Passkey.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { useEffect, useState } from "react";
import { isWebAuthnAvailable } from "@junobuild/core";
import { Button } from "../Button.jsx";
import { CreatePasskey } from "./CreatePasskey.jsx";
import { UsePasskey } from "./UsePasskey.jsx";
import { Backdrop } from "../Backdrop.jsx";

export const Passkey = () => {
// Default to true because we assume passkeys are nowadays most often supported
const [passkeySupported, setPasskeySupported] = useState(true);

const [showModal, setShowModal] = useState(false);
const [progress, setProgress] = useState(undefined);

useEffect(() => {
isWebAuthnAvailable().then((withWebAuthn) =>
setPasskeySupported(withWebAuthn),
);
}, []);

const start = () => {
setProgress(undefined);
setShowModal(true);
};

const close = () => {
setShowModal(false);
setProgress(undefined);
};

const onProgress = (progress) => {
setProgress(progress);
};

return (
<>
{passkeySupported && (
<Button onClick={start}>Continue with Passkey</Button>
)}

{showModal ? (
<>
<div
className="animate-fade fixed inset-0 z-50 p-16 md:px-24 md:py-44"
role="dialog"
aria-modal="true"
aria-labelledby="modalTitle"
>
<div className="w-full max-w-md rounded-sm border-[3px] border-black bg-white px-4 py-3 shadow-[5px_5px_0px_rgba(0,0,0,1)]">
{(progress === undefined || "setup" in progress) && (
<div className="flex items-start justify-between">
<h2
id="modalTitle"
className="text-xl font-bold text-gray-900 sm:text-2xl"
>
Hey 👋
</h2>

<button
type="button"
className="-me-4 -mt-4 rounded-full p-2 text-gray-400 transition-colors hover:bg-gray-50 hover:text-gray-600 focus:outline-none"
aria-label="Close"
onClick={close}
>
<svg
xmlns="http://www.w3.org/2000/svg"
className="size-5"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</div>
)}

<div className="mt-4">
<CreatePasskey onProgress={onProgress} progress={progress} />

<UsePasskey onProgress={onProgress} progress={progress} />
</div>
</div>
</div>
<Backdrop />
</>
) : null}
</>
);
};
38 changes: 38 additions & 0 deletions templates/react-example/src/components/passkey/Progress.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import PropTypes from "prop-types";

export const Progress = ({ children }) => {
return (
<div className="flex flex-col items-center justify-center gap-2 pt-6">
<svg
className="animate-spin"
viewBox="0 0 64 64"
fill="none"
xmlns="http://www.w3.org/2000/svg"
width="48"
height="48"
>
<path
d="M32 3C35.8083 3 39.5794 3.75011 43.0978 5.20749C46.6163 6.66488 49.8132 8.80101 52.5061 11.4939C55.199 14.1868 57.3351 17.3837 58.7925 20.9022C60.2499 24.4206 61 28.1917 61 32C61 35.8083 60.2499 39.5794 58.7925 43.0978C57.3351 46.6163 55.199 49.8132 52.5061 52.5061C49.8132 55.199 46.6163 57.3351 43.0978 58.7925C39.5794 60.2499 35.8083 61 32 61C28.1917 61 24.4206 60.2499 20.9022 58.7925C17.3837 57.3351 14.1868 55.199 11.4939 52.5061C8.801 49.8132 6.66487 46.6163 5.20749 43.0978C3.7501 39.5794 3 35.8083 3 32C3 28.1917 3.75011 24.4206 5.2075 20.9022C6.66489 17.3837 8.80101 14.1868 11.4939 11.4939C14.1868 8.80099 17.3838 6.66487 20.9022 5.20749C24.4206 3.7501 28.1917 3 32 3L32 3Z"
stroke="currentColor"
strokeWidth="5"
strokeLinecap="round"
strokeLinejoin="round"
></path>
<path
d="M32 3C36.5778 3 41.0906 4.08374 45.1692 6.16256C49.2477 8.24138 52.7762 11.2562 55.466 14.9605C58.1558 18.6647 59.9304 22.9531 60.6448 27.4748C61.3591 31.9965 60.9928 36.6232 59.5759 40.9762"
stroke="currentColor"
strokeWidth="5"
strokeLinecap="round"
strokeLinejoin="round"
className="text-lavender-blue-500"
></path>
</svg>

<p className="max-w-1/2 pb-6 text-center">{children}</p>
</div>
);
};

Progress.propTypes = {
children: PropTypes.node.isRequired,
};
78 changes: 78 additions & 0 deletions templates/react-example/src/components/passkey/UsePasskey.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,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,
};
6 changes: 5 additions & 1 deletion templates/react-ts-example/src/components/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "react";
import { Login } from "./Login";
import { Logout } from "./Logout";
import { Passkey } from "./passkey/Passkey.tsx";

export const AuthContext = createContext<{ user: User | null }>({ user: null });

Expand All @@ -30,7 +31,10 @@ export const Auth: FC<PropsWithChildren> = (props) => {
<Logout />
</div>
) : (
<Login />
<div className="gap flex flex-col">
<Passkey />
<Login />
</div>
)}
</AuthContext.Provider>
);
Expand Down
Loading
Loading