Files
neckfireandClaude Opus 4.8 742703a36f
Build & Deploy / build (push) Successful in 23s
StreakFit web: exercices planifiés en séance + filtre matériel + config matos profil
- session/[id]: affiche les exercices planifies de la routine (objectif series x reps) + pre-remplit la 1re serie
- exercises/index: select Materiel + bouton 'Mon matos' (filtre sur le materiel du profil)
- profile: multi-select du materiel possede + PATCH /api/me
- i18n fr/en/pt-BR

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-21 13:27:45 +02:00

73 lines
2.2 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
available_equipment?: string[]
}
export const useAuthStore = defineStore('auth', () => {
const user = ref<SfUser | null>(null)
const streak = ref<{ current: number, longest: number, freezes: number } | null>(null)
const push = ref<{ vapid_public_key: string | null, enabled: boolean }>({ vapid_public_key: null, enabled: false })
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, push: typeof push.value }>('/api/me')
user.value = res.user
streak.value = res.streak
if (res.push) push.value = res.push
} 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, push, ready, isAuthed, fetchMe, login, register, logout, loginWithPocketId }
})