feat: change password

This commit is contained in:
efrilm
2025-08-19 15:35:18 +07:00
parent 7919825955
commit 1b1e8c5bb4
14 changed files with 1247 additions and 119 deletions
@@ -1,5 +1,6 @@
import 'dart:developer';
import 'package:dartz/dartz.dart';
import 'package:data_channel/data_channel.dart';
import 'package:injectable/injectable.dart';
@@ -47,4 +48,36 @@ class UserRemoteDataProvider {
return DC.error(UserFailure.serverError(e));
}
}
Future<DC<UserFailure, Unit>> changePassword({
required String newPassword,
required String currentPassword,
required String userId,
}) async {
try {
final response = await _apiClient.put(
'${ApiPath.user}/$userId/password',
data: {
'new_password': newPassword,
'current_password': currentPassword,
},
headers: getAuthorizationHeader(),
);
if (response.statusCode == 400) {
return DC.error(UserFailure.unexpectedError());
}
if (response.statusCode != 200) {
return DC.error(
UserFailure.dynamicErrorMessage(response.data['error']),
);
}
return DC.data(unit);
} on ApiFailure catch (e, s) {
log('changePassword', name: _logName, error: e, stackTrace: s);
return DC.error(UserFailure.serverError(e));
}
}
}
@@ -39,4 +39,28 @@ class UserRepository implements IUserRepository {
return left(const UserFailure.unexpectedError());
}
}
@override
Future<Either<UserFailure, Unit>> changePassword({
required String newPassword,
required String currentPassword,
}) async {
try {
User currentUser = await _authLocalDataProvider.currentUser();
final result = await _dataProvider.changePassword(
newPassword: newPassword,
currentPassword: currentPassword,
userId: currentUser.id,
);
if (result.hasError) {
return left(result.error!);
}
return right(unit);
} catch (e, s) {
log('changePasswordError', name: _logName, error: e, stackTrace: s);
return left(const UserFailure.unexpectedError());
}
}
}