feat: add SQLite data layer, local auth and security settings
- Add offline-first SQLite architecture with DatabaseHelper singleton, 12 CREATE TABLE statements and full seed data on first launch - Add 10 models (fromMap/toMap) and 12 DAOs with CRUD + getPendientes() - Add ApiAuthService (JWT via SharedPreferences) and Sincronizador skeleton - Move login auth from SQLite to SharedPreferences with SHA-256 PIN hash; default PIN 123456, platform-independent (no native plugin required) - Add seguridad_page.dart: change PIN form (current → new → confirm) - Add Cerrar Sesión with confirmation dialog and full stack clear - Redesign settings_page.dart with AppColors dark theme; add Sistema section with "Inicializar base de datos" and "Inicializar sincronización" - Fix Linux desktop: sqflite_common_ffi with platform detection in DatabaseHelper - Fix ScrollController conflict on Linux: primary: false on all IndexedStack pages - Update plan_proyecto.md with session 2–3 progress and auth roadmap Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
8f13d57839
commit
1184ab5fe2
@@ -0,0 +1,70 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// Gestiona la autenticación contra la API y el ciclo de vida del token JWT.
|
||||
/// El token NUNCA se expone a las páginas — solo lo usa [Sincronizador].
|
||||
class ApiAuthService {
|
||||
static const _keyToken = 'api_token';
|
||||
static const _keyExpiry = 'api_token_expiry';
|
||||
|
||||
ApiAuthService._();
|
||||
static final ApiAuthService instance = ApiAuthService._();
|
||||
|
||||
// ── Login ──────────────────────────────────────────────────────────────────
|
||||
/// Solicita token al servidor.
|
||||
/// Devuelve `true` si el login fue exitoso.
|
||||
Future<bool> login(String baseUrl, String usuario, String password) async {
|
||||
try {
|
||||
final resp = await http.post(
|
||||
Uri.parse('$baseUrl/auth/login'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'usuario': usuario, 'password': password}),
|
||||
).timeout(const Duration(seconds: 10));
|
||||
|
||||
if (resp.statusCode == 200) {
|
||||
final body = jsonDecode(resp.body) as Map<String, dynamic>;
|
||||
final token = body['token'] as String?;
|
||||
final expiryStr = body['expiry'] as String?;
|
||||
if (token == null) return false;
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_keyToken, token);
|
||||
if (expiryStr != null) {
|
||||
await prefs.setString(_keyExpiry, expiryStr);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ── Logout ─────────────────────────────────────────────────────────────────
|
||||
Future<void> logout() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove(_keyToken);
|
||||
await prefs.remove(_keyExpiry);
|
||||
}
|
||||
|
||||
// ── Estado ─────────────────────────────────────────────────────────────────
|
||||
Future<bool> isAuthenticated() async {
|
||||
final token = await getToken();
|
||||
if (token == null || token.isEmpty) return false;
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final expiryStr = prefs.getString(_keyExpiry);
|
||||
if (expiryStr == null) return true; // Sin expiración = válido
|
||||
|
||||
final expiry = DateTime.tryParse(expiryStr);
|
||||
if (expiry == null) return true;
|
||||
return DateTime.now().isBefore(expiry);
|
||||
}
|
||||
|
||||
// ── Token ──────────────────────────────────────────────────────────────────
|
||||
Future<String?> getToken() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getString(_keyToken);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user