Files
lockaway/frontend/src/components/CreateSecretPopup.vue
T
2026-07-19 16:43:30 +02:00

70 lines
1.8 KiB
Vue

<script setup lang="ts">
import { ref } from 'vue'
import PopupTemplate from './popup/PopupTemplate.vue'
import SecretForm from './SecretForm.vue'
import { usePopup } from './popup/Popup'
import { useMasterPassword } from '../composables/useMasterPassword'
import { encryptValue } from '../crypto/secretCrypto'
import { createSecret } from '../api/secrets'
import { extractErrorMessage } from '../api/errors'
const { onCreated } = usePopup().data<{ onCreated: () => void }>()
const { derivedKey } = useMasterPassword()
const name = ref('')
const namespaces = ref<string[]>([])
const entries = ref([{ key: '', value: '' }])
const type = ref('Opaque')
const isSubmitting = ref(false)
const errorMessage = ref('')
function submit() {
const key = derivedKey.value
if (!key) {
return
}
isSubmitting.value = true
errorMessage.value = ''
Promise.all(
entries.value
.filter((entry) => entry.key)
.map((entry) => encryptValue(key, entry.value).then((encrypted) => [entry.key, encrypted] as const)),
)
.then((encryptedPairs) =>
createSecret({
name: name.value,
namespaces: namespaces.value,
entries: Object.fromEntries(encryptedPairs),
type: type.value,
}),
)
.then(() => {
onCreated()
usePopup().close()
})
.catch((error) => {
errorMessage.value = extractErrorMessage(error)
})
.finally(() => {
isSubmitting.value = false
})
}
</script>
<template>
<PopupTemplate heading="Secret erstellen">
<SecretForm
v-model:name="name"
v-model:namespaces="namespaces"
v-model:entries="entries"
v-model:type="type"
submit-label="Erstellen"
:submitting="isSubmitting"
:error-message="errorMessage"
@submit="submit"
/>
</PopupTemplate>
</template>