From f156f52608f18051355c2f887b7dbb5158afad9e Mon Sep 17 00:00:00 2001
From: Julien Nahum
Date: Tue, 16 Jan 2024 13:27:54 +0100
Subject: [PATCH] Fix form password
---
client/composables/useOpnApi.js | 6 ++---
client/pages/forms/[slug]/index.vue | 37 +++++++++++++++++++----------
client/stores/forms.js | 6 +++++
3 files changed, 33 insertions(+), 16 deletions(-)
diff --git a/client/composables/useOpnApi.js b/client/composables/useOpnApi.js
index 9b16641..41433dd 100644
--- a/client/composables/useOpnApi.js
+++ b/client/composables/useOpnApi.js
@@ -8,10 +8,10 @@ function addAuthHeader(request, options) {
}
function addPasswordToFormRequest(request, options) {
- const url = request.url
- if (!url || !url.startsWith('/forms/')) return
+ if (!request || !request.startsWith('/forms/')) return
+
+ const slug = request.split('/')[2]
- const slug = url.split('/')[3]
const passwordCookie = useCookie('password-' + slug, {maxAge: 60 * 60 * 24 * 30}) // 30 days
if (slug !== undefined && slug !== '' && passwordCookie.value !== undefined) {
options.headers['form-password'] = passwordCookie.value
diff --git a/client/pages/forms/[slug]/index.vue b/client/pages/forms/[slug]/index.vue
index 5e6046e..9c6b37c 100644
--- a/client/pages/forms/[slug]/index.vue
+++ b/client/pages/forms/[slug]/index.vue
@@ -40,7 +40,7 @@
-
@@ -66,6 +66,8 @@ const slug = useRoute().params.slug
const form = computed(() => formsStore.getByKey(slug))
const submitted = ref(false)
+const openCompleteForm = ref(null)
+
crisp.hideChat()
onBeforeRouteLeave((to, from) => {
crisp.showChat()
@@ -73,26 +75,35 @@ onBeforeRouteLeave((to, from) => {
})
const passwordEntered = function (password) {
+ console.log('passwordEntered', password, sha256(password))
useCookie('password-' + slug, {
- maxAge: {expires: 60 * 60 * 7},
+ maxAge: 60 * 60 * 7,
sameSite: false,
secure: true
}).value = sha256(password)
- loadForm(slug).then(() => {
+ loadForm().then(() => {
if (form.value?.is_password_protected) {
- this.$refs['open-complete-form'].addPasswordError('Invalid password.')
+ console.log(openCompleteForm.value)
+ openCompleteForm.value.addPasswordError('Invalid password.')
}
})
}
-const loadForm = async () => {
- if (formsStore.loading || form.value) return Promise.resolve()
- const {data, error} = await formsStore.publicLoad(slug)
- if (error.value) {
- formsStore.stopLoading()
- return
+const loadForm = async (setup=false) => {
+ console.log('loadForm',setup)
+ if (formsStore.loading || (form.value && !form.value.is_password_protected)) return Promise.resolve()
+
+ if (setup) {
+ const {data, error} = await formsStore.publicLoad(slug)
+ if (error.value) {
+ formsStore.stopLoading()
+ return
+ }
+ formsStore.save(data.value)
+ } else {
+ const data = await formsStore.publicFetch(slug)
+ formsStore.save(data)
}
- formsStore.save(data.value)
formsStore.stopLoading()
// Adapt page to form: colors, custom code etc
@@ -108,10 +119,10 @@ const loadForm = async () => {
}
onMounted(() => {
- loadForm(slug)
+ loadForm()
})
-await loadForm(slug)
+await loadForm(true)
useOpnSeoMeta({
title: () => {
diff --git a/client/stores/forms.js b/client/stores/forms.js
index c289737..ceb33e7 100644
--- a/client/stores/forms.js
+++ b/client/stores/forms.js
@@ -50,6 +50,11 @@ export const useFormsStore = defineStore('forms', () => {
return useOpnApi('/forms/' + slug)
}
+ const publicFetch = (slug) => {
+ contentStore.startLoading()
+ return opnFetch('/forms/' + slug)
+ }
+
const allTags = computed(() => {
let tags = []
contentStore.getAll.value.forEach((form) => {
@@ -69,6 +74,7 @@ export const useFormsStore = defineStore('forms', () => {
allLoaded,
allTags,
publicLoad,
+ publicFetch,
loadAll,
load,
}