Laragear / WebAuthn

Authenticate users with Passkeys: fingerprints, patterns and biometric data.
MIT License
305 stars 37 forks source link

[1.2.1] No way to handle validation errors #48

Closed tobz-nz closed 8 months ago

tobz-nz commented 1 year ago

PHP & Platform

8.2.2

Database

No response

Laravel version

10.13.5

Have you done this?

Expectation

On a login/register options request, if validation fails, errors should be availabe in the catch(resposne => {}) method.

Description

On a login/register options request, if validation fails, there is no way to display the specific errors to the user.

Reproduction

new WebAuthn().login({
    email: 'not a valid email',
}, {
    remember: 'on',
}).then(response => {
    // 
})
    .catch(response => {

    if (reponse.status === 422) {

        // response.errors should be an array of errors, and response.message should be the main validation message

        let errors = response.errors;
        for (let key in errors) {
            document.getElementById(key).setCustomValidity(errors[key][0]);
            document.getElementById(key).reportValidity();
        }

        return;
    }

})

Stack trace & logs

No response

tobz-nz commented 1 year ago

I found a pretty quick/easy solution for this:

    // webauthn.js
    async login(request = {}, response = {}) {
        const optionsResponse = await this.#fetch(request, this.#routes.loginOptions);
        const json = await optionsResponse.json();

+        if (optionsResponse.status > 400) {
+            json.status = optionsResponse.status;
+            throw json;
+        }

        ...

Could maybe be made a little tidier but does the trick. I guess adding the same thing to the register method as well.

DarkGhostHunter commented 8 months ago

Yeah. Not a bug, because the response is received regardless of the code. Yep, you have to throw the error manually in Javascript.