30-04-2026

This commit is contained in:
Kevin Adametz 2026-04-30 14:54:39 +02:00
parent 761b1156c1
commit d054732bf5
35 changed files with 2796 additions and 505 deletions

View file

@ -0,0 +1,67 @@
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
export const AUTH_STORAGE_KEY = 'thatsme-auth'
export const DEMO_USERS = Array.from({ length: 5 }, (_, index) => {
const number = index + 1
return {
id: `demo-user-${number}`,
email: `user${number}@thats-me.app`,
password: 'pass',
name: `User ${number}`,
avatar: `U${number}`
}
})
function loadStoredUserId() {
try {
const stored = localStorage.getItem(AUTH_STORAGE_KEY)
return stored ? JSON.parse(stored)?.userId ?? null : null
} catch {
return null
}
}
export const useAuthStore = defineStore('auth', () => {
const currentUserId = ref(loadStoredUserId())
const lastError = ref('')
const currentUser = computed(() =>
DEMO_USERS.find(user => user.id === currentUserId.value) ?? null
)
const isAuthenticated = computed(() => currentUser.value !== null)
function login(email, password) {
const normalizedEmail = String(email).trim().toLowerCase()
const user = DEMO_USERS.find(candidate =>
candidate.email === normalizedEmail && candidate.password === password
)
if (!user) {
lastError.value = 'E-Mail oder Passwort ist falsch.'
return false
}
currentUserId.value = user.id
lastError.value = ''
localStorage.setItem(AUTH_STORAGE_KEY, JSON.stringify({ userId: user.id }))
return true
}
function logout() {
currentUserId.value = null
lastError.value = ''
localStorage.removeItem(AUTH_STORAGE_KEY)
}
return {
users: DEMO_USERS,
currentUserId,
currentUser,
isAuthenticated,
lastError,
login,
logout
}
})