Add use page
Some checks failed
/ build (push) Failing after 35s

This commit is contained in:
Pal Kerecsenyi 2025-02-11 12:43:36 +00:00
parent 2d86494d83
commit b3b32d6221
Signed by: palk
GPG Key ID: 6891661E25394C2C
3 changed files with 47 additions and 4 deletions

View File

@ -1,19 +1,25 @@
function createChallenge() {
const challenge = new Uint8Array(256);
crypto.getRandomValues(challenge);
return challenge;
}
export async function webauthnCreate() {
const userId = new Uint8Array(8);
crypto.getRandomValues(userId);
const challenge = new Uint8Array(256);
crypto.getRandomValues(challenge);
const challenge = createChallenge();
const credential = await navigator.credentials.create({
publicKey: {
challenge,
rp: { id: location.host, name: "NativeKey (Demo)" },
rp: { id: location.host, name: "NativeKey Demo" },
user: {
id: userId,
name: "Test User",
displayName: "Test User",
},
attestation: "none",
pubKeyCredParams: [
{
type: "public-key",
@ -25,11 +31,23 @@ export async function webauthnCreate() {
},
],
authenticatorSelection: {
userVerification: "required",
residentKey: "required",
userVerification: "preferred",
},
},
});
console.log(credential);
}
export async function webauthnUse() {
const credential = await navigator.credentials.get({
publicKey: {
challenge: createChallenge(),
rpId: location.host,
userVerification: "preferred",
},
});
console.log(credential);
}

23
src/pages/use.tsx Normal file
View File

@ -0,0 +1,23 @@
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import Button from "../components/Button";
import Heading from "../components/Heading";
import { faFingerprint } from "@fortawesome/free-solid-svg-icons";
import { useCallback } from "react";
import { webauthnUse } from "../data/webauthn";
export default function UsePage() {
const onUseClick = useCallback(async () => {
await webauthnUse();
}, []);
return (
<>
<Heading>Sign in to RandomSite</Heading>
<Button onClick={onUseClick} className="mt-4">
<FontAwesomeIcon icon={faFingerprint} className="me-4" />
Sign in with your passkey
</Button>
</>
);
}

View File

@ -1,6 +1,7 @@
import { BrowserRouter, Outlet, Route, Routes } from "react-router";
import CreatePage from "./pages/create";
import Container from "./components/Container";
import UsePage from "./pages/use";
export default function Router() {
return (
@ -15,6 +16,7 @@ export default function Router() {
}
>
<Route path="/create" element={<CreatePage />} />
<Route path="/use" element={<UsePage />} />
</Route>
</Routes>
</BrowserRouter>