From 5560b6daf674d9a29f843acf9d5ff6dac83ba673 Mon Sep 17 00:00:00 2001 From: Alf Drollinger Date: Wed, 1 May 2024 23:37:46 +0200 Subject: [PATCH] Passkey JS --- packages/passkey/public/js/passkey.js | 82 +++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 packages/passkey/public/js/passkey.js diff --git a/packages/passkey/public/js/passkey.js b/packages/passkey/public/js/passkey.js new file mode 100644 index 000000000..79a2b883e --- /dev/null +++ b/packages/passkey/public/js/passkey.js @@ -0,0 +1,82 @@ +import { + startAuthentication, + startRegistration, + browserSupportsWebAuthn, +} from "@simplewebauthn/browser"; +import "./bootstrap"; + +document.addEventListener("alpine:init", () => { + Alpine.data("authForm", () => ({ + mode: "login", + username: "", + browserSupported: browserSupportsWebAuthn(), + error: null, + submit() { + this.error = null; + + if (this.mode === "login") { + return this.submitLogin(); + } + + return this.submitRegister(); + }, + submitRegister() { + window.axios + // Ask for the registration options + .post("/registration/options", { + username: this.username, + }) + // Prompt the user to create a passkey + .then((response) => startRegistration(response.data)) + // Verify the data with the server + .then((attResp) => axios.post("/registration/verify", attResp)) + .then((verificationResponse) => { + if (verificationResponse.data?.verified) { + // If we're good, reload the page and + // the server will redirect us to the dashboard + return window.location.reload(); + } + + this.error = + "Something went wrong verifying the registration."; + }) + .catch((error) => { + this.error = error?.response?.data?.message || error; + }); + }, + submitLogin() { + window.axios + // Ask for the authentication options + .post("/authentication/options", { + username: this.username, + }) + // Prompt the user to authenticate with their passkey + .then((response) => startAuthentication(response.data)) + // Verify the data with the server + .then((attResp) => + axios.post("/authentication/verify", attResp) + ) + .then((verificationResponse) => { + // If we're good, reload the page and + // the server will redirect us to the dashboard + if (verificationResponse.data?.verified) { + return window.location.reload(); + } + + this.error = + "Something went wrong verifying the authentication."; + }) + .catch((error) => { + const errorMessage = + error?.response?.data?.message || error; + + if (errorMessage === "User not found") { + this.mode = "confirmRegistration"; + return; + } + + this.error = error?.response?.data?.message || error; + }); + }, + })); +});