added api for forgot password

This commit is contained in:
llsajjad
2025-09-16 15:51:56 +03:30
parent 09ac79fd79
commit 69980925ad
13 changed files with 886 additions and 130 deletions
+61 -11
View File
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:tm_app/data/services/model/customer/customer.dart';
import 'package:tm_app/data/services/model/forgot_password_response/forgot_password_response_model.dart';
import '../core/utils/result.dart';
import '../data/repositories/auth_repository.dart';
@@ -10,14 +11,12 @@ class AuthViewModel with ChangeNotifier {
final AuthRepository _authRepository;
AuthViewModel({required AuthRepository authRepository})
: _authRepository = authRepository {
: _authRepository = authRepository {
usernameController.addListener(_onTextChanged);
passwordController.addListener(_onTextChanged);
emailController.addListener(_onTextChanged);
otpController.addListener(_onTextChanged);
newPasswordController.addListener(_onTextChanged);
// usernameFocus.addListener(_onTextChanged);
// passwordFocus.addListener(_onTextChanged);
}
final TextEditingController usernameController = TextEditingController();
@@ -25,6 +24,7 @@ class AuthViewModel with ChangeNotifier {
final TextEditingController emailController = TextEditingController();
final TextEditingController newPasswordController = TextEditingController();
final TextEditingController otpController = TextEditingController();
final FocusNode usernameFocus = FocusNode();
final FocusNode passwordFocus = FocusNode();
final FocusNode emailFocus = FocusNode();
@@ -33,25 +33,36 @@ class AuthViewModel with ChangeNotifier {
bool _obscurePassword = true;
bool get obscurePassword => _obscurePassword;
bool _obscureForgotPassword = true;
bool get obscureForgotPassword => _obscureForgotPassword;
bool get isLoginEnabled =>
usernameController.text.isNotEmpty && passwordController.text.isNotEmpty;
usernameController.text.isNotEmpty &&
passwordController.text.isNotEmpty;
// Auth state
bool _isLoading = false;
String? _errorMessage;
Customer? _loggedInUser;
String? successMessage;
String? _forgotPasswordMessage;
bool get isLoading => _isLoading;
String? get errorMessage => _errorMessage;
Customer? get loggedInUser => _loggedInUser;
String? get forgotPasswordMessage => _forgotPasswordMessage;
void togglePasswordVisibility() {
_obscurePassword = !_obscurePassword;
notifyListeners();
}
void toggleForgotPasswordVisibility() {
_obscureForgotPassword = !_obscureForgotPassword;
notifyListeners();
}
bool get isEmailValid {
final email = emailController.text.trim();
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
@@ -78,11 +89,6 @@ class AuthViewModel with ChangeNotifier {
return hasMinLength && hasUppercase && hasLowercase && hasSpecialChar;
}
void toggleForgotPasswordVisibility() {
_obscureForgotPassword = !_obscureForgotPassword;
notifyListeners();
}
void _onTextChanged() {
notifyListeners();
}
@@ -102,8 +108,6 @@ class AuthViewModel with ChangeNotifier {
switch (result) {
case Ok<LoginResponseModel>():
_loggedInUser = result.value.data.customer;
usernameController.text.trim();
passwordController.text.trim();
usernameController.clear();
passwordController.clear();
break;
@@ -114,6 +118,8 @@ class AuthViewModel with ChangeNotifier {
_isLoading = false;
notifyListeners();
// reset error after notify
_errorMessage = null;
notifyListeners();
}
@@ -140,16 +146,60 @@ class AuthViewModel with ChangeNotifier {
}
notifyListeners();
// reset error after notify
_errorMessage = null;
notifyListeners();
}
Future<void> forgotPassword(BuildContext context) async {
_isLoading = true;
_errorMessage = null;
successMessage = null;
notifyListeners();
final result = await _authRepository.forgotPassword(
email: emailController.text.trim(),
);
switch (result) {
case Ok<ForgotPasswordResponseModel>():
_isLoading = false;
successMessage = result.value.message;
notifyListeners();
// پاک شدن بعد از یک بار مصرف
Future.microtask(() {
successMessage = null;
});
break;
case Error<ForgotPasswordResponseModel>():
_isLoading = false;
_errorMessage = result.error.toString();
notifyListeners();
Future.microtask(() {
_errorMessage = null;
});
break;
}
}
@override
void dispose() {
usernameController.dispose();
passwordController.dispose();
emailController.dispose();
newPasswordController.dispose();
otpController.dispose();
usernameFocus.dispose();
passwordFocus.dispose();
emailFocus.dispose();
newPasswordFocus.dispose();
otpFocus.dispose();
super.dispose();
}
}