yii2-netdisk/web/js/login-core.js
Chenx221 07caef2555
Web Authn(3/3)
修改登录逻辑,实现WebAuthn验证
2024-03-19 17:11:41 +08:00

47 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const elemSuccess = document.getElementById('webauthn_success');
const elemError = document.getElementById('webauthn_error');
const { startAuthentication } = SimpleWebAuthnBrowser;
const elemBegin_v = document.getElementById('webauthn_verify');
const username_input = document.getElementById('username');
const remember = document.getElementById('rememberMe');
elemBegin_v.addEventListener('click', async () => {
elemSuccess.innerHTML = '';
elemError.innerHTML = '';
elemSuccess.parentElement.hidden = true;
elemError.parentElement.hidden = true;
const username = encodeURIComponent(username_input.value);
const resp = await fetch(`index.php?r=user%2Frequest-assertion-options&username=${username}`);
let asseResp;
try {
asseResp = await startAuthentication(await resp.json());
} catch (error) {
elemError.innerText = error;
elemError.parentElement.hidden = false;
throw error;
}
const isChecked = remember.checked? 1 : 0;
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
const verificationResp = await fetch(`index.php?r=user%2Fverify-assertion&is_login=1&remember=${isChecked}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': csrfToken
},
body: JSON.stringify(asseResp),
});
const verificationJSON = await verificationResp.json();
if (verificationJSON && verificationJSON.verified) {
elemSuccess.innerHTML = '登录成功1s后跳转到首页';
elemSuccess.parentElement.hidden = false;
setTimeout(() => {
window.location.href = 'index.php';
}, 1000);
} else {
elemError.innerHTML = `Oh no, something went wrong! Response: <pre>${JSON.stringify(
verificationJSON,
)}</pre>`;
elemError.parentElement.hidden = false;
}
});