feat(portal): login, dashboard, event switcher, password reset flow

Made-with: Cursor
This commit is contained in:
2026-04-13 00:52:04 +02:00
parent ec4ba8733d
commit 34eb790b3e
16 changed files with 1151 additions and 394 deletions

View File

@@ -1,61 +1,106 @@
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { apiClient } from '@/lib/axios'
interface PortalUser {
id: string
first_name: string
last_name: string
full_name: string
email: string
}
import type { AuthMeUser } from '@/types/portal'
const TOKEN_KEY = 'crewli_portal_token'
export const useAuthStore = defineStore('auth', () => {
const token = ref<string | null>(localStorage.getItem(TOKEN_KEY))
const user = ref<PortalUser | null>(null)
const user = ref<AuthMeUser | null>(null)
const isInitialized = ref(false)
const isAuthenticated = computed(() => !!token.value && !!user.value)
const isAuthenticated = computed(() => !!user.value)
function setToken(newToken: string) {
function setToken(newToken: string | null) {
token.value = newToken
localStorage.setItem(TOKEN_KEY, newToken)
if (newToken)
localStorage.setItem(TOKEN_KEY, newToken)
else
localStorage.removeItem(TOKEN_KEY)
}
function setUser(data: PortalUser) {
function setUser(data: AuthMeUser | null) {
user.value = data
}
function logout() {
token.value = null
user.value = null
localStorage.removeItem(TOKEN_KEY)
async function resetPortalStoresSync(): Promise<void> {
const { usePortalStore } = await import('@/stores/usePortalStore')
usePortalStore().reset()
}
async function fetchUser(): Promise<boolean> {
if (!token.value) {
setUser(null)
return false
}
try {
const { data } = await apiClient.get<{ success: boolean; data: AuthMeUser }>('/auth/me')
setUser(data.data)
return true
}
catch {
setToken(null)
setUser(null)
await resetPortalStoresSync()
return false
}
}
async function login(email: string, password: string): Promise<void> {
const { data } = await apiClient.post<{
success: boolean
data: { user: AuthMeUser; token: string }
}>('/auth/login', { email, password })
setToken(data.data.token)
setUser(data.data.user)
const ok = await fetchUser()
if (!ok) throw new Error('Sessie kon niet worden gestart.')
}
function clearLocalSession(): void {
setToken(null)
setUser(null)
void resetPortalStoresSync()
}
async function logout(): Promise<void> {
try {
if (token.value)
await apiClient.post('/auth/logout')
}
catch {
// Ignore network errors; still clear local session
}
setToken(null)
setUser(null)
await resetPortalStoresSync()
}
let initializePromise: Promise<void> | null = null
function initialize(): Promise<void> {
if (isInitialized.value) return Promise.resolve()
if (!initializePromise) {
if (!initializePromise)
initializePromise = doInitialize()
}
return initializePromise
}
async function doInitialize(): Promise<void> {
if (!token.value) {
isInitialized.value = true
return
}
try {
const { data } = await apiClient.get<{ success: boolean; data: PortalUser }>('/auth/me')
setUser(data.data)
}
catch {
logout()
await fetchUser()
}
finally {
isInitialized.value = true
@@ -69,7 +114,10 @@ export const useAuthStore = defineStore('auth', () => {
isInitialized,
setToken,
setUser,
login,
logout,
fetchUser,
initialize,
clearLocalSession,
}
})