Created
March 6, 2024 16:17
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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