profile_service.dart 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import 'dart:convert';
  2. import 'package:http/http.dart' as http;
  3. import 'package:yoshi_papas_app/data/session/session_storage.dart';
  4. import '../models/models.dart';
  5. import '../services/base_service.dart';
  6. class ProfileService extends BaseService {
  7. String endPoint = 'v1/usuario';
  8. // Método existente para consultar perfil
  9. Future<Profile> fetchList() async {
  10. final int? id = await SessionStorage().getId();
  11. if (id == null) {
  12. throw Exception(
  13. "Información de perfil no disponible, por favor inicia sesión.");
  14. }
  15. Map<String, String> queryParameters = {
  16. 'id': id.toString(),
  17. };
  18. final response = await get(endPoint, queryParameters: queryParameters);
  19. final profileJson = jsonDecode(response.body);
  20. final profile = Profile.fromJson(profileJson['resultado'][0]);
  21. return profile;
  22. }
  23. // Método actualizado para cambiar la contraseña
  24. Future<bool> cambiarContrasena(String claveActual, String nuevaClave) async {
  25. // Obtener el token de autenticación
  26. final String? token = await SessionStorage().getToken();
  27. // Obtener el ID del usuario
  28. final int? id = await SessionStorage().getId();
  29. if (token == null || id == null) {
  30. print("Token o ID del usuario no disponible.");
  31. return false;
  32. }
  33. final url = Uri.parse('$base_url/$endPoint/cambiar-clave');
  34. final response = await http.post(
  35. url,
  36. headers: {
  37. "Content-Type": "application/json",
  38. "Authorization":
  39. "Bearer $token", // Incluye el token en la cabecera de autorización
  40. },
  41. body: jsonEncode({
  42. "id": id, // Incluye el ID del usuario en el cuerpo de la solicitud
  43. "claveActual": claveActual,
  44. "clave": nuevaClave,
  45. }),
  46. );
  47. if (response.statusCode == 200) {
  48. // Operación exitosa
  49. return true;
  50. } else {
  51. // Operación fallida
  52. print('Error al cambiar la contraseña: ${response.body}');
  53. return false;
  54. }
  55. }
  56. }