Replace composable with Pinia store for persistent auth state

This commit is contained in:
Torsten Schulz (local)
2025-10-21 14:19:30 +02:00
parent 1015d37eb7
commit 43071b45a9
10 changed files with 137 additions and 69 deletions

58
stores/auth.js Normal file
View File

@@ -0,0 +1,58 @@
import { defineStore } from 'pinia'
export const useAuthStore = defineStore('auth', {
state: () => ({
isLoggedIn: false,
user: null,
role: null
}),
getters: {
isAdmin: (state) => {
return state.role === 'admin' || state.role === 'vorstand'
}
},
actions: {
async checkAuth() {
try {
const response = await $fetch('/api/auth/status')
this.isLoggedIn = response.isLoggedIn
this.user = response.user
this.role = response.role
return response
} catch (error) {
this.isLoggedIn = false
this.user = null
this.role = null
return { isLoggedIn: false }
}
},
async login(email, password) {
const response = await $fetch('/api/auth/login', {
method: 'POST',
body: { email, password }
})
if (response.success) {
await this.checkAuth()
}
return response
},
async logout() {
try {
await $fetch('/api/auth/logout', { method: 'POST' })
this.isLoggedIn = false
this.user = null
this.role = null
} catch (error) {
console.error('Logout fehlgeschlagen:', error)
throw error
}
}
}
})