1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import 'dart:convert';
- import 'package:http/http.dart' as http;
- import 'package:yoshi_papas_app/data/session/session_storage.dart';
- import '../models/models.dart';
- import '../services/base_service.dart';
- class ProfileService extends BaseService {
- String endPoint = 'v1/usuario';
- // Método existente para consultar perfil
- Future<Profile> fetchList() async {
- final int? id = await SessionStorage().getId();
- if (id == null) {
- throw Exception(
- "Información de perfil no disponible, por favor inicia sesión.");
- }
- Map<String, String> queryParameters = {
- 'id': id.toString(),
- };
- final response = await get(endPoint, queryParameters: queryParameters);
- final profileJson = jsonDecode(response.body);
- final profile = Profile.fromJson(profileJson['resultado'][0]);
- return profile;
- }
- // Método actualizado para cambiar la contraseña
- Future<bool> cambiarContrasena(String claveActual, String nuevaClave) async {
- // Obtener el token de autenticación
- final String? token = await SessionStorage().getToken();
- // Obtener el ID del usuario
- final int? id = await SessionStorage().getId();
- if (token == null || id == null) {
- print("Token o ID del usuario no disponible.");
- return false;
- }
- final url = Uri.parse('$base_url/$endPoint/cambiar-clave');
- final response = await http.post(
- url,
- headers: {
- "Content-Type": "application/json",
- "Authorization":
- "Bearer $token", // Incluye el token en la cabecera de autorización
- },
- body: jsonEncode({
- "id": id, // Incluye el ID del usuario en el cuerpo de la solicitud
- "claveActual": claveActual,
- "clave": nuevaClave,
- }),
- );
- if (response.statusCode == 200) {
- // Operación exitosa
- return true;
- } else {
- // Operación fallida
- print('Error al cambiar la contraseña: ${response.body}');
- return false;
- }
- }
- }
|