Skip to content

Instantly share code, notes, and snippets.

@mingderwang
Created March 6, 2024 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingderwang/88becfb7ecbbf54626370a8d0c54dc40 to your computer and use it in GitHub Desktop.
Save mingderwang/88becfb7ecbbf54626370a8d0c54dc40 to your computer and use it in GitHub Desktop.
refer to https://simplewebauthn.dev/docs/packages/browser (code is generated by ChatGPT in openAI)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web Authentication Example</title>
</head>
<body>
<script>
async function createCredentials() {
try {
const publicKeyCredentialCreationOptions = {
challenge: new Uint8Array(32), // replace with a real challenge from your server
rp: {
name: 'Example Corporation',
id: 'runkit.sh',
},
user: {
id: new Uint8Array(16), // replace with a real user id
name: 'john.doe@example.com',
displayName: 'John Doe',
},
pubKeyCredParams: [
{
alg: -7,
type: "public-key"
},
{
"alg": -257,
"type": "public-key"
}
],
};
const newCredential = await navigator.credentials.create({
publicKey: publicKeyCredentialCreationOptions,
});
// Handle the new credential as needed, e.g., send it to the server for verification
console.log('New credential created:', newCredential);
} catch (error) {
console.error('Error creating credentials:', error);
}
}
// Call the function when the page is loaded
createCredentials();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment