38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
import {useRegistration, useLogin} from '@web-auth/webauthn-helper'
|
|
|
|
const webauthn_register = useRegistration({
|
|
actionUrl: "/user/webauthn/",
|
|
optionsUrl: "/user/webauthn/options",
|
|
})
|
|
|
|
const webauthn_login = useLogin({
|
|
actionUrl: "/login/webauthn/",
|
|
optionsUrl: "/login/webauthn/options",
|
|
})
|
|
|
|
window.webauthn_register = webauthn_register;
|
|
window.webauthn_login = webauthn_login;
|
|
(function() {
|
|
const button = window.document.getElementById("do_webauthn_login");
|
|
if (button) {
|
|
button.addEventListener("click", function (e) {
|
|
const loginForm = window.document.getElementById("webauthn_login_form");
|
|
if (!loginForm) return;
|
|
e.preventDefault();
|
|
const formData = new FormData(loginForm)
|
|
webauthn_login({
|
|
username: formData.get("username")
|
|
})
|
|
.then(() => {
|
|
// 成功登录
|
|
window.location.href = "/"
|
|
})
|
|
.catch((err) => {
|
|
console.error(err)
|
|
alert("登录失败");
|
|
})
|
|
return false;
|
|
})
|
|
}
|
|
})()
|