Skip to content

Instantly share code, notes, and snippets.

@MeiyappanKannappa
Created March 11, 2024 14:42
Show Gist options
  • Select an option

  • Save MeiyappanKannappa/ffd6337d20a137b546d91161e394406d to your computer and use it in GitHub Desktop.

Select an option

Save MeiyappanKannappa/ffd6337d20a137b546d91161e394406d to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebAuthn Registration</title>
</head>
<body>
<h1>WebAuthn Registration</h1>
<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
</div>
<div>
<button id="register-button">Register</button>
<button id="login-button">Login</button>
</div>
<script>
document.getElementById('register-button').addEventListener('click', async () => {
const username = document.getElementById('username').value;
// Simulate the server response with registration options
const registrationOptions = {
challenge: new Uint8Array(32),
rp: { name: 'Example Corp' },
user: {
id: new TextEncoder().encode(username),
name: username,
displayName: username
},
pubKeyCredParams: [
{ type: 'public-key', alg: -7 }
]
};
// Use the WebAuthn API to create a new credential
const credential = await navigator.credentials.create({ publicKey: registrationOptions });
// Simulate sending the new credential to the server for verification and storage
console.log('Credential:', credential);
alert('Registration successful!');
});
document.getElementById('login-button').addEventListener('click', async () => {
const username = document.getElementById('username').value;
// Simulate the server response with login options
const loginOptions = {
challenge: new Uint8Array(32),
rpId: '7054-103-190-144-234.ngrok-free.app'
};
// Use the WebAuthn API to get a credential for login
const credential = await navigator.credentials.get({ publicKey: loginOptions });
// Simulate sending the credential to the server for verification
console.log('Credential:', credential);
alert('Login successful!');
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment