yunohost-admin/app/src/views/tool/ToolAdminpw.vue

76 lines
2 KiB
Vue
Raw Normal View History

2020-09-12 19:02:12 +02:00
<template>
<password-form
:title="$t('postinstall_set_password')"
:server-error="serverError"
@submit="onSubmit"
:extra="extra"
>
<template #extra-fields="{ v, fields, form }">
<!-- CURRENT ADMIN PASSWORD -->
<form-field v-bind="fields.currentPassword" v-model="form.currentPassword" :validation="v.form.currentPassword" />
<hr>
</template>
</password-form>
2020-09-12 19:02:12 +02:00
</template>
<script>
2020-10-12 17:36:47 +02:00
import api from '@/api'
import { validationMixin } from 'vuelidate'
import { PasswordForm } from '@/components/reusableForms'
import { required, minLength } from '@/helpers/validators'
2020-09-12 19:02:12 +02:00
export default {
name: 'ToolAdminpw',
data () {
return {
serverError: '',
extra: {
form: {
currentPassword: ''
},
fields: {
currentPassword: {
label: this.$i18n.t('tools_adminpw_current'),
description: this.$i18n.t('tools_adminpw_current_placeholder'),
props: { id: 'current-password', type: 'password', placeholder: '••••••••' }
}
},
validations: {
currentPassword: { required, passwordLenght: minLength(8) }
}
2020-09-12 19:02:12 +02:00
}
}
},
methods: {
onSubmit ({ password, currentPassword }) {
this.serverError = ''
// Use `api.fetch` to avoid automatic redirect on 401 (Unauthorized).
api.fetch('POST', 'login', { password: currentPassword }).then(response => {
if (response.status === 401) {
// Dispatch `SERVER_RESPONDED` to hide waiting overlay and display error.
this.$store.dispatch('SERVER_RESPONDED', true)
this.serverError = this.$i18n.t('wrong_password')
} else if (response.ok) {
api.put('adminpw', { new_password: password }).then(() => {
this.$store.dispatch('DISCONNECT')
}).catch(error => {
this.serverError = error.message
})
}
2020-09-12 19:02:12 +02:00
})
}
},
mixins: [validationMixin],
2020-09-12 19:02:12 +02:00
components: {
PasswordForm
2020-09-12 19:02:12 +02:00
}
}
</script>