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 loginForm = window.document.getElementById("webauthn_login_form");
    console.log(loginForm)
    if (loginForm) {
        loginForm.addEventListener("submit", function (e) {
            e.preventDefault();
            const formData = new FormData(loginForm)
            webauthn_login(formData)
                .then(() => {
                    // 成功登录
                    window.location.href = "/"
                })
                .catch((err) => {
                    console.error(err)
                    alert("登录失败");
                })
            return false;
        })
    }
})()