- OperadorLoginPage con auth WebControl 2 pasos (empresas → login) - ShellPage: sidebar 220px + IndexedStack + header con estado API - DashboardPage: stat cards, feed alarmas, clientes, incidentes (timer 30s) - ApiAuthService: flujo real con email + paswd + dba → token + apiKey - DB SQLite v1: 7 tablas + 7 DAOs completos - theme.dart: AppColors, appHeadline (Manrope), appLabel (Inter) - Logo app: escudo radar neón (SVG + PNG 512px) en lib/recursos/ - desplegar.sh: build release Linux + upload a siax-system.net - agents.md: estado completo + objetivos futuros documentados Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
143 lines
5.5 KiB
Dart
143 lines
5.5 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
/// Flujo de autenticación WebControl (2 pasos):
|
|
///
|
|
/// Paso 1 — POST /api/auth/empresas { email, paswd }
|
|
/// → { success, usuario, empresas:[{dba,nombre,rol}], unica }
|
|
///
|
|
/// Paso 2 — POST /api/auth/login { email, paswd, dba }
|
|
/// → { success, token, apiKey, db_tenant, usuario:{id,nombre,usuario,rol} }
|
|
///
|
|
/// Uso posterior (llamadas a api.telcotronics.com):
|
|
/// final h = await ApiAuthService.instance.getCentinelHeaders();
|
|
class ApiAuthService {
|
|
static const _base = 'https://api.siax-system.net';
|
|
|
|
static const _keyToken = 'webcontrol_token';
|
|
static const _keyApiKey = 'centinel_api_key';
|
|
static const _keyTenant = 'centinel_tenant';
|
|
static const _keyNombre = 'operador_nombre';
|
|
static const _keyUsuario = 'operador_usuario';
|
|
|
|
ApiAuthService._();
|
|
static final ApiAuthService instance = ApiAuthService._();
|
|
|
|
// ── Paso 1: obtener empresas del usuario ──────────────────────
|
|
Future<List<Map<String, dynamic>>?> getEmpresas(
|
|
String email, String paswd) async {
|
|
try {
|
|
final resp = await http.post(
|
|
Uri.parse('$_base/api/auth/empresas'),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'accept': 'application/json',
|
|
},
|
|
body: jsonEncode({'email': email, 'paswd': paswd}),
|
|
).timeout(const Duration(seconds: 10));
|
|
|
|
if (resp.statusCode != 200) return null;
|
|
final body = jsonDecode(resp.body) as Map<String, dynamic>;
|
|
if (body['success'] != true) return null;
|
|
|
|
return List<Map<String, dynamic>>.from(
|
|
body['empresas'] as List? ?? []);
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// ── Paso 2: login completo con dba ────────────────────────────
|
|
Future<bool> login(String email, String paswd, {String? dba}) async {
|
|
try {
|
|
// Paso 1: obtener dba si no se pasó
|
|
String? dbaTenant = dba;
|
|
if (dbaTenant == null) {
|
|
final empresas = await getEmpresas(email, paswd);
|
|
if (empresas == null || empresas.isEmpty) return false;
|
|
dbaTenant = empresas.first['dba'] as String?;
|
|
if (dbaTenant == null) return false;
|
|
}
|
|
|
|
// Paso 2: login real
|
|
final resp = await http.post(
|
|
Uri.parse('$_base/api/auth/login'),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'accept': 'application/json',
|
|
},
|
|
body: jsonEncode({'email': email, 'paswd': paswd, 'dba': dbaTenant}),
|
|
).timeout(const Duration(seconds: 10));
|
|
|
|
if (resp.statusCode != 200) return false;
|
|
|
|
final body = jsonDecode(resp.body) as Map<String, dynamic>;
|
|
if (body['success'] != true) return false;
|
|
|
|
final token = body['token'] as String?;
|
|
final apiKey = body['apiKey'] as String?;
|
|
final tenant = body['db_tenant'] as String?;
|
|
final usr = body['usuario'] as Map<String, dynamic>?;
|
|
final nombre = usr?['nombre'] as String? ?? email;
|
|
final usuario = usr?['usuario'] as String? ?? email;
|
|
|
|
if (apiKey == null || tenant == null) return false;
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
if (token != null) await prefs.setString(_keyToken, token);
|
|
await prefs.setString(_keyApiKey, apiKey);
|
|
await prefs.setString(_keyTenant, tenant);
|
|
await prefs.setString(_keyNombre, nombre);
|
|
await prefs.setString(_keyUsuario, usuario);
|
|
return true;
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// ── Logout ────────────────────────────────────────────────────
|
|
Future<void> logout() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.remove(_keyToken);
|
|
await prefs.remove(_keyApiKey);
|
|
await prefs.remove(_keyTenant);
|
|
await prefs.remove(_keyNombre);
|
|
await prefs.remove(_keyUsuario);
|
|
}
|
|
|
|
// ── Estado ────────────────────────────────────────────────────
|
|
Future<bool> isAuthenticated() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final apiKey = prefs.getString(_keyApiKey);
|
|
return apiKey != null && apiKey.isNotEmpty;
|
|
}
|
|
|
|
// ── Headers para api.telcotronics.com ─────────────────────────
|
|
Future<Map<String, String>?> getCentinelHeaders() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final apiKey = prefs.getString(_keyApiKey);
|
|
final tenant = prefs.getString(_keyTenant);
|
|
if (apiKey == null || apiKey.isEmpty) return null;
|
|
return {
|
|
'Content-Type': 'application/json',
|
|
'accept': 'application/json',
|
|
'x-api-key': apiKey,
|
|
'x-database': tenant ?? '',
|
|
};
|
|
}
|
|
|
|
// ── Getters ───────────────────────────────────────────────────
|
|
Future<String?> getNombre() async =>
|
|
(await SharedPreferences.getInstance()).getString(_keyNombre);
|
|
|
|
Future<String?> getUsuario() async =>
|
|
(await SharedPreferences.getInstance()).getString(_keyUsuario);
|
|
|
|
Future<String?> getApiKey() async =>
|
|
(await SharedPreferences.getInstance()).getString(_keyApiKey);
|
|
|
|
Future<String?> getToken() async =>
|
|
(await SharedPreferences.getInstance()).getString(_keyToken);
|
|
}
|