feat: multi-usuário com autenticação JWT

- Tabela `profiles` + coluna `profile_id` em todas as entidades
  (categories, transactions, recurring_expenses, accounts, player_profile,
   xp_events, player_quests, player_achievements, player_cosmetics)
- Dados existentes migrados para profile_id = 1 (Manoel)
- CLI `./api create-user --name <n> --password <p>` cria perfil com
  seed de categorias e player_profile; faz upsert de senha se já existir
- Auth substituída: cookie+APP_PASSWORD → JWT Bearer 24h (HS256)
- Middleware RequireAuth injeta profile_id no context de todas as rotas
- Todos os repositórios filtram por profile_id do context
- Endpoints: POST /api/auth/login, GET /api/auth/me,
  POST /api/auth/change-password, POST /api/logout
- Frontend: auth store usa localStorage (fc_token/fc_profile),
  api.ts envia Authorization header, LoginView usa campo name
- SettingsView reescrita com troca de senha e logout
- docker-compose.yml: remove APP_USERNAME/APP_PASSWORD, adiciona JWT_SECRET

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
2026-05-27 20:04:44 -03:00
co-authored by Claude Sonnet 4.6
parent 50637bd590
commit acbce4edc0
49 changed files with 2662 additions and 382 deletions
@@ -0,0 +1,47 @@
-- Multi-user support: profiles table + profile_id in all per-user entities
CREATE TABLE IF NOT EXISTS profiles (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE,
password_hash VARCHAR(72) NOT NULL DEFAULT '',
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);
-- Seed profile 1 for the existing data owner (Manoel)
INSERT INTO profiles (id, name, password_hash) VALUES (1, 'manoel', '')
ON CONFLICT DO NOTHING;
SELECT setval('profiles_id_seq', GREATEST(1, (SELECT MAX(id) FROM profiles)));
-- Add profile_id to all per-user tables (DEFAULT 1 makes existing rows belong to Manoel)
ALTER TABLE categories ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id);
ALTER TABLE transactions ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id);
ALTER TABLE recurring_expenses ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id);
ALTER TABLE accounts ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id);
ALTER TABLE player_profile ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id);
ALTER TABLE xp_events ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id);
ALTER TABLE player_quests ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id);
ALTER TABLE player_achievements ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id);
ALTER TABLE player_cosmetics ADD COLUMN IF NOT EXISTS profile_id INTEGER NOT NULL DEFAULT 1 REFERENCES profiles(id);
-- Fix unique constraints to be scoped per profile
ALTER TABLE player_quests
DROP CONSTRAINT IF EXISTS player_quests_quest_id_period_key;
ALTER TABLE player_quests
ADD CONSTRAINT player_quests_quest_id_period_profile_key
UNIQUE (quest_id, period, profile_id);
ALTER TABLE player_achievements
DROP CONSTRAINT IF EXISTS player_achievements_achievement_id_key;
ALTER TABLE player_achievements
ADD CONSTRAINT player_achievements_achievement_id_profile_key
UNIQUE (achievement_id, profile_id);
ALTER TABLE player_cosmetics
DROP CONSTRAINT IF EXISTS player_cosmetics_cosmetic_id_key;
ALTER TABLE player_cosmetics
ADD CONSTRAINT player_cosmetics_cosmetic_id_profile_key
UNIQUE (cosmetic_id, profile_id);
INSERT INTO schema_migrations (version) VALUES (14) ON CONFLICT DO NOTHING;