From 22b5527fdebdf641d717952a67bb4bb9a2d4c2fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 30 Jul 2023 19:39:37 +0200 Subject: [PATCH] Handle invalid input in the new account form --- src/assets/main.sass | 2 ++ src/views/AddAccountView.vue | 33 +++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/assets/main.sass b/src/assets/main.sass index 6278c17..bc0e195 100644 --- a/src/assets/main.sass +++ b/src/assets/main.sass @@ -9,7 +9,9 @@ // Elements @import "node_modules/bulma/sass/elements/button" @import "node_modules/bulma/sass/elements/container" +@import "node_modules/bulma/sass/elements/notification" @import "node_modules/bulma/sass/elements/title" +@import "node_modules/bulma/sass/elements/other" // Form @import "node_modules/bulma/sass/form/shared" diff --git a/src/views/AddAccountView.vue b/src/views/AddAccountView.vue index b5641ae..d4eee17 100644 --- a/src/views/AddAccountView.vue +++ b/src/views/AddAccountView.vue @@ -11,22 +11,27 @@ const localPart = ref(''); const separator = ref('+'); const domainName = ref(''); const privateKey = ref(''); +const errorMessage = ref(''); const base64Decode = (str_b64) => { - const raw_str = atob(str_b64); - const length = raw_str.length; - var b = []; - for (var i = 0; i < length; i++) { - b.push(raw_str.charCodeAt(i)); + try { + const raw_str = atob(str_b64); + const length = raw_str.length; + var b = []; + for (var i = 0; i < length; i++) { + b.push(raw_str.charCodeAt(i)); + } + return b; + } catch (e) { + throw new Error('The key must be a valid base64 string.'); } - return b; }; // Add account button const addDisabled = computed(() => { const params = [ localPart.value, - separator.value.length == 1, + separator.value, domainName.value, privateKey.value ]; @@ -35,6 +40,9 @@ const addDisabled = computed(() => { const addAccount = () => { if (!addDisabled.value) { try { + if (separator.value.length != 1) { + throw new Error('The separator must be a single character.'); + } const key = base64Decode(privateKey.value); const hash = sha256(`${localPart.value}@${domainName.value}`); const newAccount = { @@ -47,7 +55,7 @@ const addAccount = () => { accounts.value.push(newAccount); return toMainView(); } catch (e) { - console.log(e); + errorMessage.value = e.message; } } }; @@ -59,10 +67,19 @@ const cancellDisabled = computed(() => { const toMainView = () => { return router.push('/'); }; + +// Error message +const resetErrorMessage = () => { + errorMessage.value = ''; +};