refactor: migrate to new changePassword endpoint (#31277)
Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
This commit is contained in:
co-authored by
shenlong-tanwen
parent
db6a5b0d56
commit
d2928cecce
@@ -115,9 +115,9 @@ class AuthNotifier extends StateNotifier<AuthState> {
|
||||
state = state.copyWith(profileImagePath: path);
|
||||
}
|
||||
|
||||
Future<bool> changePassword(String newPassword) async {
|
||||
Future<bool> changePassword({required String currentPassword, required String newPassword}) async {
|
||||
try {
|
||||
await _authService.changePassword(newPassword);
|
||||
await _authService.changePassword(currentPassword: currentPassword, newPassword: newPassword);
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
|
||||
@@ -12,8 +12,10 @@ class AuthApiRepository extends ApiRepository {
|
||||
|
||||
AuthApiRepository(this._apiService);
|
||||
|
||||
Future<void> changePassword(String newPassword) async {
|
||||
await _apiService.usersApi.updateMyUser(UserUpdateMeDto(password: Optional.present(newPassword)));
|
||||
Future<void> changePassword({required String currentPassword, required String newPassword}) async {
|
||||
await _apiService.authenticationApi.changePassword(
|
||||
ChangePasswordDto(password: currentPassword, newPassword: newPassword),
|
||||
);
|
||||
}
|
||||
|
||||
Future<LoginResponse> login(String email, String password) async {
|
||||
|
||||
@@ -129,9 +129,9 @@ class AuthService {
|
||||
]);
|
||||
}
|
||||
|
||||
Future<void> changePassword(String newPassword) {
|
||||
Future<void> changePassword({required String currentPassword, required String newPassword}) async {
|
||||
try {
|
||||
return _authApiRepository.changePassword(newPassword);
|
||||
await _authApiRepository.changePassword(currentPassword: currentPassword, newPassword: newPassword);
|
||||
} catch (error, stackTrace) {
|
||||
_log.severe("Error changing password", error, stackTrace);
|
||||
rethrow;
|
||||
|
||||
@@ -14,6 +14,7 @@ class ChangePasswordForm extends HookConsumerWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final currentPasswordController = useTextEditingController.fromValue(TextEditingValue.empty);
|
||||
final passwordController = useTextEditingController.fromValue(TextEditingValue.empty);
|
||||
final confirmPasswordController = useTextEditingController.fromValue(TextEditingValue.empty);
|
||||
final authState = ref.watch(authProvider);
|
||||
@@ -43,7 +44,11 @@ class ChangePasswordForm extends HookConsumerWidget {
|
||||
key: formKey,
|
||||
child: Column(
|
||||
children: [
|
||||
PasswordInput(controller: passwordController),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 16.0),
|
||||
child: PasswordInput(controller: currentPasswordController, label: context.t.password),
|
||||
),
|
||||
PasswordInput(controller: passwordController, label: context.t.change_password_form_new_password),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
||||
child: ConfirmPasswordInput(
|
||||
@@ -57,7 +62,10 @@ class ChangePasswordForm extends HookConsumerWidget {
|
||||
if (formKey.currentState!.validate()) {
|
||||
final isSuccess = await ref
|
||||
.read(authProvider.notifier)
|
||||
.changePassword(passwordController.value.text);
|
||||
.changePassword(
|
||||
currentPassword: currentPasswordController.text,
|
||||
newPassword: passwordController.text,
|
||||
);
|
||||
|
||||
if (!isSuccess && context.mounted) {
|
||||
ImmichToast.show(
|
||||
@@ -107,19 +115,16 @@ class ChangePasswordForm extends HookConsumerWidget {
|
||||
|
||||
class PasswordInput extends StatelessWidget {
|
||||
final TextEditingController controller;
|
||||
final String label;
|
||||
|
||||
const PasswordInput({super.key, required this.controller});
|
||||
const PasswordInput({super.key, required this.controller, required this.label});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return TextFormField(
|
||||
obscureText: true,
|
||||
controller: controller,
|
||||
decoration: InputDecoration(
|
||||
labelText: context.t.change_password_form_new_password,
|
||||
border: const OutlineInputBorder(),
|
||||
hintText: context.t.change_password_form_new_password,
|
||||
),
|
||||
decoration: InputDecoration(labelText: label, border: const OutlineInputBorder(), hintText: label),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,6 +264,7 @@ class LoginForm extends HookConsumerWidget {
|
||||
|
||||
if (result.shouldChangePassword && !result.isAdmin) {
|
||||
unawaited(context.pushRoute(const ChangePasswordRoute()));
|
||||
passwordController.clear();
|
||||
} else {
|
||||
await ref.read(galleryPermissionNotifier.notifier).requestGalleryPermission();
|
||||
if (isSyncRemoteDeletionsMode()) {
|
||||
|
||||
Reference in New Issue
Block a user