Files
leopost-full/frontend/src/api.js
Michele 77ca70cd48 feat: multi-user SaaS, piani Freemium/Pro, Google OAuth, admin panel
BLOCCO 1 - Multi-user data model:
- User: email, display_name, avatar_url, auth_provider, google_id
- User: subscription_plan, subscription_expires_at, is_admin, post counters
- SubscriptionCode table per redeem codes
- user_id FK su Character, Post, AffiliateLink, EditorialPlan, SocialAccount, SystemSetting
- Migrazione SQLite-safe (ALTER TABLE) + preserva dati esistenti

BLOCCO 2 - Auth completo:
- Registrazione email/password + login multi-user
- Google OAuth 2.0 (httpx, no deps esterne)
- Callback flow: Google -> /auth/callback?token=JWT -> frontend
- Backward compat login admin con username

BLOCCO 3 - Piani e abbonamenti:
- Freemium: 1 character, 15 post/mese, FB+IG only, no auto-plans
- Pro: illimitato, tutte le piattaforme, tutte le feature
- Enforcement automatico in tutti i router
- Redeem codes con durate 1/3/6/12 mesi
- Admin panel: genera codici, lista utenti

BLOCCO 4 - Frontend completo:
- Login page design Leopost (split coral/cream, Google, social coming soon)
- AuthCallback per OAuth redirect
- PlanBanner, UpgradeModal con pricing
- AdminSettings per generazione codici
- CharacterForm con tab Account Social + guide setup

Deploy:
- Dockerfile con ARG VITE_BASE_PATH/VITE_API_BASE
- docker-compose.prod.yml per leopost.it (no subpath)
- docker-compose.yml aggiornato per lab

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-31 20:01:07 +02:00

45 lines
1.3 KiB
JavaScript

const BASE_URL = import.meta.env.VITE_API_BASE || '/api'
async function request(method, path, body = null) {
const headers = { 'Content-Type': 'application/json' }
const token = localStorage.getItem('token')
if (token) headers['Authorization'] = `Bearer ${token}`
const res = await fetch(`${BASE_URL}${path}`, {
method,
headers,
body: body ? JSON.stringify(body) : null,
})
if (res.status === 401) {
localStorage.removeItem('token')
const basePath = import.meta.env.VITE_BASE_PATH || '/leopost-full'
window.location.href = basePath ? `${basePath}/login` : '/login'
return
}
if (!res.ok) {
const error = await res.json().catch(() => ({ detail: 'Request failed' }))
// Pass through structured errors (upgrade_required etc.)
const detail = error.detail
if (typeof detail === 'object' && detail !== null) {
const err = new Error(detail.message || 'Request failed')
err.data = detail
throw err
}
throw new Error(detail || 'Request failed')
}
if (res.status === 204) return null
return res.json()
}
export const api = {
get: (path) => request('GET', path),
post: (path, body) => request('POST', path, body),
put: (path, body) => request('PUT', path, body),
delete: (path) => request('DELETE', path),
}
export { BASE_URL }