diff --git a/resources/js/services/userService.js b/resources/js/services/userService.js index 6fcb98ce..5ad84e3e 100644 --- a/resources/js/services/userService.js +++ b/resources/js/services/userService.js @@ -4,6 +4,15 @@ const apiClient = httpClientFactory('api') const webClient = httpClientFactory('web') export default { + /** + * Get current signed-in user preferences + * + * @returns promise + */ + getPreferences(config = {}) { + return apiClient.get('/user/preferences', { ...config }) + }, + /** * Update a user preference * diff --git a/resources/js/stores/user.js b/resources/js/stores/user.js index 444ae24a..74090626 100644 --- a/resources/js/stores/user.js +++ b/resources/js/stores/user.js @@ -1,5 +1,6 @@ import { defineStore } from 'pinia' import authService from '@/services/authService' +import userService from '@/services/userService' import router from '@/router' import { useColorMode } from '@vueuse/core' import { useTwofaccounts } from '@/stores/twofaccounts' @@ -108,7 +109,6 @@ export const useUserStore = defineStore({ mode.value = this.preferences.theme == 'system' ? 'auto' : this.preferences.theme }, - /** * Applies user language */ @@ -121,13 +121,27 @@ export const useUserStore = defineStore({ else loadLanguageAsync('en') }, - /** * Applies both user theme & language */ applyUserPrefs() { this.applyTheme() this.applyLanguage() + }, + + /** + * Refresh user preferences with backend state + */ + refreshPreferences() { + userService.getPreferences({returnError: true}) + .then(response => { + response.data.forEach(preference => { + this.preferences[preference.key] = preference.value + }) + }) + .catch(error => { + notify.alert({ text: trans('errors.data_cannot_be_refreshed_from_server') }) + }) } }, diff --git a/resources/js/views/settings/Options.vue b/resources/js/views/settings/Options.vue index 64179817..71d81348 100644 --- a/resources/js/views/settings/Options.vue +++ b/resources/js/views/settings/Options.vue @@ -78,6 +78,8 @@ }) } }) + + user.refreshPreferences() }) /** diff --git a/resources/lang/en/errors.php b/resources/lang/en/errors.php index 907dd4e1..de638aef 100644 --- a/resources/lang/en/errors.php +++ b/resources/lang/en/errors.php @@ -65,4 +65,5 @@ return [ 'sso_no_register' => 'Registrations are disabled', 'sso_email_already_used' => 'A user account with the same email address already exists but it does not match your external account ID. Do not use SSO if you are already registered on 2FAuth with this email.', 'account_managed_by_external_provider' => 'Account managed by an external provider', + 'data_cannot_be_refreshed_from_server' => 'Data cannot be refreshed from server' ]; \ No newline at end of file