<!DOCTYPE html>
<html>
<head>
<title>Hawcx Login</title>
<style>
body { font-family: sans-serif; max-width: 400px; margin: 50px auto; }
input { width: 100%; padding: 8px; margin: 10px 0; box-sizing: border-box; }
button { width: 100%; padding: 10px; background: #0076e0; color: white; border: none; cursor: pointer; }
</style>
</head>
<body>
<h1>Login</h1>
<input type="email" id="email" placeholder="Email">
<button onclick="handleLogin()">Sign In</button>
<div id="status"></div>
<script type="module">
import { HawcxInitializer } from 'https://drprpwzor5vmy.cloudfront.net/v1.1.55/hawcx-auth.esm.min.js';
let hawcx;
let currentEmail;
// Initialize
async function init() {
hawcx = await HawcxInitializer.init('YOUR_API_KEY', "your-base-url");
document.getElementById('status').textContent = 'Ready';
}
// Handle login
window.handleLogin = async function() {
currentEmail = document.getElementById('email').value;
if (!currentEmail) return alert('Enter email');
try {
const response = await hawcx.authenticate(currentEmail);
if (response.status === 'SUCCESS') {
// Trusted device
await exchangeCode(currentEmail, response.code);
} else if (response.status === 'OTP_NEEDED') {
// New device - get email OTP
document.getElementById('status').textContent = 'Check email for OTP';
const emailOtp = prompt('Email OTP:');
await hawcx.verifyEmailOtp({ userid: currentEmail, otp: emailOtp });
// Complete registration
const result = await hawcx.verifyDevice({
userid: currentEmail,
mfaMethod: 'sms' // 'email', 'sms', or 'totp'
});
if (result.code || result.status === 'SUCCESS' || result.status === 'DEVICE_REGISTERED') {
await exchangeCode(currentEmail, result.code);
}
} else if (response.status === 'MFA_REQUIRED') {
// MFA required
await hawcx.initiateMfa({ userid: currentEmail, sessionId: response.sessionId });
const mfaOtp = prompt('MFA OTP:');
const mfaResult = await hawcx.verifyMfa({
userid: currentEmail,
otp: mfaOtp,
remember_me: true
});
if (mfaResult.code) {
await exchangeCode(currentEmail, mfaResult.code);
}
}
} catch (error) {
document.getElementById('status').textContent = 'Error: ' + error.message;
}
};
// Exchange code with backend
async function exchangeCode(email, code) {
const res = await fetch('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code, email })
});
if (res.ok) {
const { sessionToken } = await res.json();
localStorage.setItem('sessionToken', sessionToken);
window.location.href = '/app';
} else {
document.getElementById('status').textContent = 'Backend auth failed';
}
}
init();
</script>
</body>
</html>