2FAuth/resources/js/stores/user.js

135 lines
3.7 KiB
JavaScript
Raw Normal View History

2023-09-22 15:07:47 +02:00
import { defineStore } from 'pinia'
import authService from '@/services/authService'
import router from '@/router'
import { useColorMode } from '@vueuse/core'
2023-10-27 15:42:58 +02:00
import { useTwofaccounts } from '@/stores/twofaccounts'
import { useGroups } from '@/stores/groups'
import { useNotifyStore } from '@/stores/notify'
2023-09-22 15:07:47 +02:00
export const useUserStore = defineStore({
id: 'user',
2023-09-22 15:07:47 +02:00
state: () => {
2023-09-22 15:07:47 +02:00
return {
name: undefined,
email: undefined,
oauth_provider: undefined,
preferences: window.defaultPreferences,
2023-09-22 15:07:47 +02:00
isAdmin: false,
}
},
getters: {
isAuthenticated() {
return this.name != undefined
}
},
actions: {
2023-10-27 15:42:58 +02:00
/**
* Initializes the store for the given user
*
* @param {object} user
*/
async loginAs(user) {
2023-10-27 15:42:58 +02:00
this.$patch(user)
await this.initDataStores()
2023-10-27 15:42:58 +02:00
this.applyUserPrefs()
},
/**
* Initializes the user's data stores
*/
async initDataStores() {
2023-10-27 15:42:58 +02:00
const accounts = useTwofaccounts()
const groups = useGroups()
if (this.isAuthenticated) {
await accounts.fetch()
groups.fetch()
2023-10-27 15:42:58 +02:00
}
else {
accounts.$reset()
groups.$reset()
}
},
2023-10-27 15:42:58 +02:00
/**
* Logs the user out or moves to proxy logout url
*/
logout(options = {}) {
const { kicked } = options
const notify = useNotifyStore()
// async appLogout(evt) {
if (this.$2fauth.config.proxyAuth) {
if (this.$2fauth.config.proxyLogoutUrl) {
location.assign(this.$2fauth.config.proxyLogoutUrl)
}
else return false
}
else {
authService.logout({ returnError: true }).then(() => {
if (kicked) {
notify.clear()
notify.warn({ text: trans('auth.autolock_triggered_punchline'), duration:-1 })
}
this.tossOut()
})
.catch(error => {
// The logout request will receive a 401 response when the
// backend has already detect inactivity on its side. In this case we
// don't want any error to be displayed.
if (error.response.status !== 401) {
notify.error(error)
}
else this.tossOut()
})
}
},
2023-10-27 15:42:58 +02:00
/**
* Resets all user data and push out
2023-10-27 15:42:58 +02:00
*/
tossOut() {
this.$reset()
2023-10-27 15:42:58 +02:00
this.initDataStores()
this.applyUserPrefs()
router.push({ name: 'login' })
},
2023-10-27 15:42:58 +02:00
/**
* Applies user theme
*/
applyTheme() {
const mode = useColorMode({
attribute: 'data-theme',
})
mode.value = this.preferences.theme == 'system' ? 'auto' : this.preferences.theme
},
2023-10-27 15:42:58 +02:00
/**
* Applies user language
*/
applyLanguage() {
const { isSupported, language } = useNavigatorLanguage()
if (isSupported) {
loadLanguageAsync(this.preferences.lang == 'browser' ? language.value.slice(0, 2) : this.preferences.lang)
}
else loadLanguageAsync('en')
},
2023-10-27 15:42:58 +02:00
/**
* Applies both user theme & language
*/
applyUserPrefs() {
this.applyTheme()
this.applyLanguage()
}
},
2023-09-22 15:07:47 +02:00
})