30-04-2026
This commit is contained in:
parent
761b1156c1
commit
d054732bf5
35 changed files with 2796 additions and 505 deletions
67
frontend/src/stores/auth.js
Normal file
67
frontend/src/stores/auth.js
Normal 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
|
||||
}
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue