diff --git a/src/stores/employee.ts b/src/stores/employee.ts index dd71558..a15d8ce 100644 --- a/src/stores/employee.ts +++ b/src/stores/employee.ts @@ -99,13 +99,40 @@ export const useEmployee = defineStore({ /** TODO: #23 Persist user if password is changed */ async persist() { try { - const result = await axios.patch('employees/'+this.employee.id, this.employee, - { - headers: { - 'Authorization': 'Bearer '+user.token + let result + + if(isNaN(this.user.id) && this.user.username.length > 0){ + result = await axios.post( + 'users', + { + username: this.user.username, + password: this.user.password + }, + { + headers: user.header } + ) + } + else if (this.user.password.length > 0 && this.user.password === this.user.passwordConfirm){ + result = await axios.patch( + 'users/'+this.user.id, + { + password: this.user.password + }, + { + headers: user.header + } + ) + } + + result = await axios.patch( + 'employees/'+this.employee.id, + this.employee, + { + headers: user.header } ) + Object.assign(this.clean.employee, this.employee) Object.assign(this.clean.user, this.user) notifications.add('success', result.statusText) @@ -115,6 +142,10 @@ export const useEmployee = defineStore({ else console.log(error) } } + }, + + getters: { + } }) diff --git a/src/stores/user.ts b/src/stores/user.ts index d444fe2..b42080a 100644 --- a/src/stores/user.ts +++ b/src/stores/user.ts @@ -27,6 +27,9 @@ export const useUser = defineStore({ getters: { isAdmin: (state) => state.role === 'admin', + header: (state) => { + return { 'Authorization': 'Bearer '+state.token } + }, }, actions: { diff --git a/src/views/EmployeesDetails.vue b/src/views/EmployeesDetails.vue index d950f9d..2427fc0 100644 --- a/src/views/EmployeesDetails.vue +++ b/src/views/EmployeesDetails.vue @@ -1,6 +1,12 @@