login_view_model.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. _name = apiResponse.detalle!['name'];
  47. _status = Status.authenticated;
  48. _cargando = false;
  49. notifyListeners();
  50. return _status;
  51. }
  52. }
  53. if (apiResponse.isError) {
  54. hasErrors = true;
  55. _errores = apiResponse.errores;
  56. _status = Status.unauthenticated;
  57. _cargando = false;
  58. notifyListeners();
  59. }
  60. } catch (e) {
  61. _status = Status.unauthenticated;
  62. _cargando = false;
  63. notifyListeners();
  64. }
  65. }
  66. void checkSession() async {
  67. var token = await SessionStorage().getToken();
  68. if (token != null && token.isNotEmpty) {
  69. _status = Status.authenticated;
  70. _idUsuario = await SessionStorage().getId();
  71. } else {
  72. _status = Status.unauthenticated;
  73. }
  74. notifyListeners();
  75. }
  76. logOut() async {
  77. await SessionStorage().clearToken();
  78. _status = Status.unauthenticated;
  79. notifyListeners();
  80. }
  81. void showPassword() {
  82. _obscureText = !_obscureText;
  83. notifyListeners();
  84. }
  85. setValores() async {
  86. _name = (await SessionStorage().getNombre()).toString();
  87. _email = (await SessionStorage().getCorreo()).toString();
  88. _empresa = (await SessionStorage().getEmpresa()).toString();
  89. notifyListeners();
  90. }
  91. bool validarEmail(String email) =>
  92. RegExp(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
  93. .hasMatch(email.trim());
  94. }