Files
streakfit-web/app/stores/auth.ts
T
neckfireandClaude Opus 4.8 8ee9596529
Build & Deploy / build (push) Failing after 5s
StreakFit — PWA Nuxt (thème flamme, logo SF animé)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-19 03:00:18 +02:00

70 lines
2.0 KiB
TypeScript

import { defineStore } from 'pinia'
/**
* Store d'auth "mince" : il ne fait que relayer vers le back et mémoriser l'utilisateur courant.
* Aucune logique métier ici (règle : tout est géré côté back).
*/
interface SfUser {
id: number
name: string
email: string
locale?: string
units?: string
avatar_url?: string | null
}
export const useAuthStore = defineStore('auth', () => {
const user = ref<SfUser | null>(null)
const streak = ref<{ current: number, longest: number, freezes: number } | null>(null)
const ready = ref(false)
const isAuthed = computed(() => !!user.value)
async function fetchMe() {
const { api } = useApi()
try {
const res = await api<{ user: SfUser, streak: typeof streak.value }>('/api/me')
user.value = res.user
streak.value = res.streak
} catch {
user.value = null
streak.value = null
} finally {
ready.value = true
}
}
async function login(email: string, password: string) {
const { api, ensureCsrf } = useApi()
await ensureCsrf()
const res = await api<{ user: SfUser }>('/login', { method: 'POST', body: { email, password } })
user.value = res.user
await fetchMe()
}
async function register(name: string, email: string, password: string) {
const { api, ensureCsrf } = useApi()
await ensureCsrf()
const res = await api<{ user: SfUser }>('/register', { method: 'POST', body: { name, email, password } })
user.value = res.user
await fetchMe()
}
async function logout() {
const { api, ensureCsrf } = useApi()
await ensureCsrf()
try { await api('/logout', { method: 'POST' }) } finally {
user.value = null
streak.value = null
}
}
/** Login OIDC pocket-id : 100% back — on redirige simplement le navigateur. */
function loginWithPocketId() {
const { base } = useApi()
window.location.href = `${base}/auth/oidc/redirect`
}
return { user, streak, ready, isAuthed, fetchMe, login, register, logout, loginWithPocketId }
})