mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-06-24 22:12:06 +02:00
Make the user store handle data stores
This commit is contained in:
parent
a52cc2dcc9
commit
46a58ecb29
@ -8,13 +8,12 @@ export default async function auth({ to, next, stores }) {
|
|||||||
if (! user.isAuthenticated) {
|
if (! user.isAuthenticated) {
|
||||||
const currentUser = await authService.getCurrentUser()
|
const currentUser = await authService.getCurrentUser()
|
||||||
if (currentUser) {
|
if (currentUser) {
|
||||||
user.$patch({
|
user.loginAs({
|
||||||
name: currentUser.name,
|
name: currentUser.name,
|
||||||
email: currentUser.email,
|
email: currentUser.email,
|
||||||
preferences: currentUser.preferences,
|
preferences: currentUser.preferences,
|
||||||
isAdmin: currentUser.is_admin,
|
isAdmin: currentUser.is_admin,
|
||||||
})
|
})
|
||||||
user.applyUserPrefs()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
48
resources/js_vue3/stores/user.js
vendored
48
resources/js_vue3/stores/user.js
vendored
@ -2,6 +2,8 @@ import { defineStore } from 'pinia'
|
|||||||
import authService from '@/services/authService'
|
import authService from '@/services/authService'
|
||||||
import router from '@/router'
|
import router from '@/router'
|
||||||
import { useColorMode } from '@vueuse/core'
|
import { useColorMode } from '@vueuse/core'
|
||||||
|
import { useTwofaccounts } from '@/stores/twofaccounts'
|
||||||
|
import { useGroups } from '@/stores/groups'
|
||||||
|
|
||||||
export const useUserStore = defineStore({
|
export const useUserStore = defineStore({
|
||||||
id: 'user',
|
id: 'user',
|
||||||
@ -22,7 +24,37 @@ export const useUserStore = defineStore({
|
|||||||
},
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
|
/**
|
||||||
|
* Initializes the store for the given user
|
||||||
|
*
|
||||||
|
* @param {object} user
|
||||||
|
*/
|
||||||
|
loginAs(user) {
|
||||||
|
this.$patch(user)
|
||||||
|
this.initDataStores()
|
||||||
|
this.applyUserPrefs()
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the user's data stores
|
||||||
|
*/
|
||||||
|
initDataStores() {
|
||||||
|
const accounts = useTwofaccounts()
|
||||||
|
const groups = useGroups()
|
||||||
|
|
||||||
|
if (this.isAuthenticated) {
|
||||||
|
accounts.fetch()
|
||||||
|
groups.fetch()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
accounts.$reset()
|
||||||
|
groups.$reset()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs the user out or moves to proxy logout url
|
||||||
|
*/
|
||||||
logout() {
|
logout() {
|
||||||
// async appLogout(evt) {
|
// async appLogout(evt) {
|
||||||
if (this.$2fauth.config.proxyAuth) {
|
if (this.$2fauth.config.proxyAuth) {
|
||||||
@ -41,13 +73,19 @@ export const useUserStore = defineStore({
|
|||||||
// },
|
// },
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets all user data
|
||||||
|
*/
|
||||||
reset() {
|
reset() {
|
||||||
localStorage.clear()
|
|
||||||
this.$reset()
|
this.$reset()
|
||||||
|
this.initDataStores()
|
||||||
this.applyUserPrefs()
|
this.applyUserPrefs()
|
||||||
router.push({ name: 'login' })
|
router.push({ name: 'login' })
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies user theme
|
||||||
|
*/
|
||||||
applyTheme() {
|
applyTheme() {
|
||||||
const mode = useColorMode({
|
const mode = useColorMode({
|
||||||
attribute: 'data-theme',
|
attribute: 'data-theme',
|
||||||
@ -55,6 +93,10 @@ export const useUserStore = defineStore({
|
|||||||
mode.value = this.preferences.theme == 'system' ? 'auto' : this.preferences.theme
|
mode.value = this.preferences.theme == 'system' ? 'auto' : this.preferences.theme
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies user language
|
||||||
|
*/
|
||||||
applyLanguage() {
|
applyLanguage() {
|
||||||
const { isSupported, language } = useNavigatorLanguage()
|
const { isSupported, language } = useNavigatorLanguage()
|
||||||
|
|
||||||
@ -64,6 +106,10 @@ export const useUserStore = defineStore({
|
|||||||
else loadLanguageAsync('en')
|
else loadLanguageAsync('en')
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies both user theme & language
|
||||||
|
*/
|
||||||
applyUserPrefs() {
|
applyUserPrefs() {
|
||||||
this.applyTheme()
|
this.applyTheme()
|
||||||
this.applyLanguage()
|
this.applyLanguage()
|
||||||
|
@ -30,13 +30,12 @@
|
|||||||
notify.clear()
|
notify.clear()
|
||||||
form.post('/user/login', {returnError: true})
|
form.post('/user/login', {returnError: true})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
user.$patch({
|
user.loginAs({
|
||||||
name: response.data.name,
|
name: response.data.name,
|
||||||
email: response.data.email,
|
email: response.data.email,
|
||||||
preferences: response.data.preferences,
|
preferences: response.data.preferences,
|
||||||
isAdmin: response.data.is_admin,
|
isAdmin: response.data.is_admin,
|
||||||
})
|
})
|
||||||
user.applyTheme()
|
|
||||||
|
|
||||||
router.push({ name: 'accounts', params: { toRefresh: true } })
|
router.push({ name: 'accounts', params: { toRefresh: true } })
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user