Authentication
Authentication usage
class User extends Authenticatableauth()->user()->idinterface LoggedUserState {
user: LoggedUser
widgetData: AuthWidgetData
loading: boolean
errorRequest: { [key: string]: Array<string> }
}
export const useLoggedUserStore = defineStore('utility-loggedUser', {
state: (): LoggedUserState => ({
user: {
id: 0,
slug: '',
name: '',
surname: '',
title: '',
email: '',
language: '',
dark_mode: false,
roles: [],
user_image: [],
confirm_new_password: '',
new_password: '',
current_password: '',
created_at: '',
updated_at: '',
deleted_at: '',
},
widgetData: {
count_orders: 0,
count_tasks: 0,
count_teams: 0,
},
loading: false,
errorRequest: {},
}),
getters: {
getUser: (state) => state.user,
getLoading: (state) => state.loading,
},
actions: {
async getLoggedUserInfo() {
try {
this.loading = true
getLoggedUserInfoResponse.value = executeSWRV(`${route}/me`)
if (!getLoggedUserInfoResponse.value.data) {
await getLoggedUserInfoResponse.value.mutate()
}
} finally {
this.loading = false
}
},
async fetchWidgetData() {
try {
this.loading = true
fetchWidgetDataResponse.value = executeSWRV(
`${route}/getWidgetData`
)
if (!fetchWidgetDataResponse.value.data) {
await fetchWidgetDataResponse.value.mutate()
}
} finally {
this.loading = false
}
},
async updatePassword() {
try {
this.loading = true
const params = objectToFormData(this.user, {
indices: true,
booleansAsIntegers: true,
})
await axios.post(`${route}/updatePassword`, params)
this.notify({
title: this.t('global.profile_update_success'),
type: 'warn',
})
this.router.back()
} catch (error: any) {
this.errorRequest = error.response.data.errors
} finally {
this.loading = false
}
},
async updateProfile() {
try {
this.loading = true
const params = objectToFormData(this.user, {
indices: true,
booleansAsIntegers: true,
})
await axios.post(`${route}/updateProfile`, params)
this.notify({
title: this.t('global.profile_update_success'),
type: 'warn',
})
this.router.back()
} catch (error: any) {
this.errorRequest = error.response.data.errors
} finally {
this.loading = false
}
},
async toggleDarkMode() {
try {
this.loading = true
const params = {
darkMode: this.user.dark_mode,
}
await axios.post(`${route}/toggleDarkMode`, params)
} catch (error: any) {
this.errorRequest = error.response.data.errors
} finally {
this.loading = false
}
},
setLang(lang: string) {
axios.post(`users/${this.user?.id}/setLang`, {
lang: lang,
})
},
resetState() {
this.$reset()
},
},
})Last updated