Add forgot password, reset password, and verify OTP providers and view models
- Implemented `ForgotPasswordViewModel`, `ResetPasswordViewModel`, and `VerifyOtpViewModel` for handling respective functionalities. - Created provider functions for lazy loading of these view models in the app. - Updated routing to utilize the new providers for managing state in the forgot password flow. - Refactored UI components to integrate with the new view models, ensuring proper state management and user feedback during operations.
This commit is contained in:
@@ -1,15 +1,10 @@
|
||||
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 'package:tm_app/data/services/model/verify_otp_response/verify_otp_response_model.dart';
|
||||
|
||||
import '../core/utils/error_utils.dart';
|
||||
import '../core/utils/logger.dart';
|
||||
import '../core/utils/result.dart';
|
||||
import '../data/repositories/auth_repository.dart';
|
||||
import '../data/services/model/login_response/login_response_model.dart';
|
||||
import '../data/services/model/reset_password_response/reset_password_response.dart';
|
||||
import '../views/forget_password_create/strings/forgot_password_create_strings.dart';
|
||||
|
||||
class AuthViewModel with ChangeNotifier {
|
||||
final AuthRepository _authRepository;
|
||||
@@ -18,101 +13,37 @@ class AuthViewModel with ChangeNotifier {
|
||||
: _authRepository = authRepository {
|
||||
usernameController.addListener(_onTextChanged);
|
||||
passwordController.addListener(_onTextChanged);
|
||||
emailController.addListener(_onTextChanged);
|
||||
otpController.addListener(_onTextChanged);
|
||||
newPasswordController.addListener(_onTextChanged);
|
||||
}
|
||||
|
||||
final TextEditingController usernameController = TextEditingController();
|
||||
final TextEditingController passwordController = TextEditingController();
|
||||
final TextEditingController emailController = TextEditingController();
|
||||
final TextEditingController newPasswordController = TextEditingController();
|
||||
final TextEditingController otpController = TextEditingController();
|
||||
|
||||
final FocusNode usernameFocus = FocusNode();
|
||||
final FocusNode passwordFocus = FocusNode();
|
||||
final FocusNode emailFocus = FocusNode();
|
||||
final FocusNode newPasswordFocus = FocusNode();
|
||||
final FocusNode otpFocus = FocusNode();
|
||||
|
||||
bool _obscurePassword = true;
|
||||
bool get obscurePassword => _obscurePassword;
|
||||
|
||||
bool _obscureForgotPassword = true;
|
||||
bool get obscureForgotPassword => _obscureForgotPassword;
|
||||
|
||||
bool get isLoginEnabled =>
|
||||
usernameController.text.isNotEmpty && passwordController.text.isNotEmpty;
|
||||
|
||||
// Auth state
|
||||
bool _isLoading = false;
|
||||
bool _isLoggedIn = false;
|
||||
bool _isResetPasswordLoading = false;
|
||||
String? _errorMessage;
|
||||
String? _resetPasswordErrorMessage;
|
||||
bool _resetPasswordSuccess = false;
|
||||
Customer? _loggedInUser;
|
||||
String? successMessage;
|
||||
String? _successMessageOtp;
|
||||
String? _forgotPasswordMessage;
|
||||
bool _isLoadingForgot = false;
|
||||
bool _isLoadingOtp = false;
|
||||
String? _errorMessageForget;
|
||||
String? _errorMessageOtp;
|
||||
bool _verifyOtpSuccess = false;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
bool get isLoggedIn => _isLoggedIn;
|
||||
bool get isLoadingForgot => _isLoadingForgot;
|
||||
bool get isLoadingOtp => _isLoadingOtp;
|
||||
bool get isResetPasswordLoading => _isResetPasswordLoading;
|
||||
String? get errorMessage => _errorMessage;
|
||||
String? get errorMessageForget => _errorMessageForget;
|
||||
String? get resetPasswordErrorMessage => _resetPasswordErrorMessage;
|
||||
String? get errorMessageOtp => _errorMessageOtp;
|
||||
String? get successMessageOtp => _successMessageOtp;
|
||||
bool get verifyOtpSuccess => _verifyOtpSuccess;
|
||||
Customer? get loggedInUser => _loggedInUser;
|
||||
String? get forgotPasswordMessage => _forgotPasswordMessage;
|
||||
String? resetToken;
|
||||
bool get resetPasswordSuccess => _resetPasswordSuccess;
|
||||
|
||||
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}$');
|
||||
return emailRegex.hasMatch(email);
|
||||
}
|
||||
|
||||
bool get isOtpValid {
|
||||
final otp = otpController.text.trim();
|
||||
return otp.length == 6;
|
||||
}
|
||||
|
||||
bool get canSubmitOtpPage {
|
||||
return isOtpValid;
|
||||
}
|
||||
|
||||
bool get isNewPasswordValid {
|
||||
final password = newPasswordController.text.trim();
|
||||
|
||||
final hasMinLength = password.length >= 5;
|
||||
final hasUppercase = RegExp(r'[A-Z]').hasMatch(password);
|
||||
final hasLowercase = RegExp(r'[a-z]').hasMatch(password);
|
||||
final hasSpecialChar = RegExp(r'[!@#\$%&\*\^]').hasMatch(password);
|
||||
|
||||
return hasMinLength && hasUppercase && hasLowercase && hasSpecialChar;
|
||||
}
|
||||
|
||||
void _onTextChanged() {
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -153,120 +84,10 @@ class AuthViewModel with ChangeNotifier {
|
||||
AppLogger().error('❌ Failed to local logout: $e');
|
||||
}
|
||||
_loggedInUser = null;
|
||||
_isLoggedIn = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> forgotPassword() async {
|
||||
_isLoadingForgot = true;
|
||||
_errorMessageForget = null;
|
||||
successMessage = null;
|
||||
notifyListeners();
|
||||
|
||||
final result = await _authRepository.forgotPassword(
|
||||
email: emailController.text.trim(),
|
||||
);
|
||||
|
||||
switch (result) {
|
||||
case Ok<ForgotPasswordResponseModel>():
|
||||
_isLoadingForgot = false;
|
||||
successMessage = result.value.message;
|
||||
|
||||
notifyListeners();
|
||||
|
||||
break;
|
||||
|
||||
case Error<ForgotPasswordResponseModel>():
|
||||
_isLoadingForgot = false;
|
||||
_errorMessageForget = result.error.toString();
|
||||
notifyListeners();
|
||||
|
||||
Future.microtask(() {
|
||||
_errorMessageForget = null;
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void clearMessages() {
|
||||
successMessage = null;
|
||||
_errorMessage = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> verifyOtp() async {
|
||||
_isLoadingOtp = true;
|
||||
_errorMessageOtp = null;
|
||||
_verifyOtpSuccess = false;
|
||||
notifyListeners();
|
||||
|
||||
final result = await _authRepository.verifyOtp(
|
||||
code: otpController.text.trim(),
|
||||
email: emailController.text.trim(),
|
||||
);
|
||||
|
||||
switch (result) {
|
||||
case Ok<VerifyOtpResponseModel>():
|
||||
_isLoadingOtp = false;
|
||||
resetToken = result.value.data.token;
|
||||
_successMessageOtp = result.value.message;
|
||||
_verifyOtpSuccess = true;
|
||||
notifyListeners();
|
||||
|
||||
break;
|
||||
|
||||
case Error<VerifyOtpResponseModel>():
|
||||
_isLoadingOtp = false;
|
||||
_errorMessageOtp = result.error.toString();
|
||||
_verifyOtpSuccess = false;
|
||||
notifyListeners();
|
||||
break;
|
||||
}
|
||||
|
||||
_errorMessageOtp = null;
|
||||
_successMessageOtp = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void clearOtpFlow() {
|
||||
_errorMessageOtp = null;
|
||||
_successMessageOtp = null;
|
||||
_isLoadingOtp = false;
|
||||
_verifyOtpSuccess = false;
|
||||
// Intentionally do not clear resetToken
|
||||
otpController.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> resetPassword() async {
|
||||
_isResetPasswordLoading = true;
|
||||
_resetPasswordErrorMessage = null;
|
||||
_resetPasswordSuccess = false;
|
||||
notifyListeners();
|
||||
if (resetToken != null) {
|
||||
final result = await _authRepository.resetPassword(
|
||||
newPassword: newPasswordController.text.trim(),
|
||||
token: resetToken!,
|
||||
);
|
||||
switch (result) {
|
||||
case Ok<ResetPasswordResponse>():
|
||||
_resetPasswordSuccess = true;
|
||||
break;
|
||||
case Error<ResetPasswordResponse>():
|
||||
_resetPasswordErrorMessage = ErrorUtils.getErrorMessage(result.error);
|
||||
_resetPasswordSuccess = false;
|
||||
break;
|
||||
}
|
||||
_isResetPasswordLoading = false;
|
||||
_resetPasswordErrorMessage = null;
|
||||
notifyListeners();
|
||||
} else {
|
||||
_resetPasswordErrorMessage =
|
||||
ForgotPasswordCreateStrings.passwordResetFailed;
|
||||
_isResetPasswordLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> checkIsLoggedIn() async {
|
||||
final result = await _authRepository.checkIsLoggedIn();
|
||||
|
||||
@@ -282,18 +103,13 @@ class AuthViewModel with ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void clearResetPasswordSuccess() {
|
||||
_resetPasswordSuccess = false;
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void clearResetFlow() {
|
||||
_resetPasswordErrorMessage = null;
|
||||
_resetPasswordSuccess = false;
|
||||
_isResetPasswordLoading = false;
|
||||
// Intentionally do not clear resetToken
|
||||
newPasswordController.clear();
|
||||
void clearAuthFlow() {
|
||||
_errorMessage = null;
|
||||
successMessage = null;
|
||||
usernameController.clear();
|
||||
passwordController.clear();
|
||||
_isLoggedIn = false;
|
||||
_loggedInUser = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -301,14 +117,8 @@ class AuthViewModel with ChangeNotifier {
|
||||
void dispose() {
|
||||
usernameController.dispose();
|
||||
passwordController.dispose();
|
||||
emailController.dispose();
|
||||
newPasswordController.dispose();
|
||||
otpController.dispose();
|
||||
usernameFocus.dispose();
|
||||
passwordFocus.dispose();
|
||||
emailFocus.dispose();
|
||||
newPasswordFocus.dispose();
|
||||
otpFocus.dispose();
|
||||
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user