login_view_model.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'package:flutter/material.dart';
  2. import '../data/api_response.dart';
  3. import '../data/session/session_storage.dart';
  4. import '../services/login_service.dart';
  5. enum Status { uninitialized, authenticated, authenticating, unauthenticated }
  6. class LoginViewModel extends ChangeNotifier {
  7. Status _status = Status.uninitialized;
  8. Status get status => _status;
  9. bool hasErrors = false;
  10. Map<String, dynamic>? _errores = {};
  11. bool _obscureText = true;
  12. String? _idUsuario;
  13. Map<String, dynamic>? get errores => _errores;
  14. bool get obscureText => _obscureText;
  15. String? get idUsuario => _idUsuario;
  16. List<String> _permisos = [];
  17. List<String> get permisos => _permisos;
  18. String name = "";
  19. String get _name => name;
  20. String email = "";
  21. String get _email => email;
  22. String _empresa = "";
  23. String get empresa => _empresa;
  24. String _error = "";
  25. String get error => _error;
  26. bool _cargando = false;
  27. bool get cargando => _cargando;
  28. setCargando(bool value) {
  29. _cargando = value;
  30. notifyListeners();
  31. }
  32. Future login(String username, String password) async {
  33. _cargando = true;
  34. notifyListeners();
  35. try {
  36. ApiResponse apiResponse = await LoginService().logIn(username, password);
  37. _errores = {};
  38. if (apiResponse.isOk) {
  39. _idUsuario = apiResponse.detalle?['id'];
  40. String token = apiResponse.detalle?['token'];
  41. if (token.isNotEmpty) {
  42. SessionStorage().saveToken(apiResponse.detalle?['token']);
  43. SessionStorage().saveId(apiResponse.detalle?['id']);
  44. SessionStorage().saveCorreo(apiResponse.detalle!['email']);
  45. SessionStorage().saveNombre(apiResponse.detalle!['name']);
  46. _status = Status.authenticated;
  47. _cargando = false;
  48. notifyListeners();
  49. return _status;
  50. }
  51. }
  52. if (apiResponse.isError) {
  53. hasErrors = true;
  54. _errores = apiResponse.errores;
  55. _status = Status.unauthenticated;
  56. _cargando = false;
  57. notifyListeners();
  58. }
  59. } catch (e) {
  60. _status = Status.unauthenticated;
  61. _cargando = false;
  62. notifyListeners();
  63. }
  64. }
  65. void checkSession() async {
  66. var token = await SessionStorage().getToken();
  67. if (token != null && token.isNotEmpty) {
  68. _status = Status.authenticated;
  69. _idUsuario = await SessionStorage().getId();
  70. } else {
  71. _status = Status.unauthenticated;
  72. }
  73. notifyListeners();
  74. }
  75. logOut() async {
  76. await SessionStorage().clearToken();
  77. _status = Status.unauthenticated;
  78. notifyListeners();
  79. }
  80. void showPassword() {
  81. _obscureText = !_obscureText;
  82. notifyListeners();
  83. }
  84. setValores() async {
  85. name = (await SessionStorage().getNombre()).toString();
  86. email = (await SessionStorage().getCorreo()).toString();
  87. _empresa = (await SessionStorage().getEmpresa()).toString();
  88. notifyListeners();
  89. }
  90. bool validarEmail(String correo) =>
  91. RegExp(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
  92. .hasMatch(correo.trim());
  93. }