feat(auth): add sendCodeSilently with isSending state
This commit is contained in:
@@ -16,6 +16,7 @@ class RegisterState extends Equatable {
|
||||
final String? errorMessage;
|
||||
final String? pendingEmail;
|
||||
final bool codeSent;
|
||||
final bool isSending;
|
||||
|
||||
const RegisterState({
|
||||
this.username = const Username.pure(),
|
||||
@@ -26,6 +27,7 @@ class RegisterState extends Equatable {
|
||||
this.errorMessage,
|
||||
this.pendingEmail,
|
||||
this.codeSent = false,
|
||||
this.isSending = false,
|
||||
});
|
||||
|
||||
bool get isStep1Valid =>
|
||||
@@ -41,6 +43,7 @@ class RegisterState extends Equatable {
|
||||
String? errorMessage,
|
||||
String? pendingEmail,
|
||||
bool? codeSent,
|
||||
bool? isSending,
|
||||
}) {
|
||||
return RegisterState(
|
||||
username: username ?? this.username,
|
||||
@@ -51,6 +54,7 @@ class RegisterState extends Equatable {
|
||||
errorMessage: errorMessage,
|
||||
pendingEmail: pendingEmail ?? this.pendingEmail,
|
||||
codeSent: codeSent ?? this.codeSent,
|
||||
isSending: isSending ?? this.isSending,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -64,6 +68,7 @@ class RegisterState extends Equatable {
|
||||
errorMessage,
|
||||
pendingEmail,
|
||||
codeSent,
|
||||
isSending,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -160,4 +165,31 @@ class RegisterCubit extends Cubit<RegisterState> {
|
||||
emit(state.copyWith(errorMessage: message));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> sendCodeSilently() async {
|
||||
if (!state.isStep1Valid) return;
|
||||
|
||||
emit(state.copyWith(isSending: true));
|
||||
|
||||
try {
|
||||
final response = await _repository.signupStart(
|
||||
SignupStartRequest(
|
||||
username: state.username.value,
|
||||
email: state.email.value,
|
||||
password: state.password.value,
|
||||
),
|
||||
);
|
||||
emit(
|
||||
state.copyWith(
|
||||
isSending: false,
|
||||
pendingEmail: response.email,
|
||||
codeSent: true,
|
||||
errorMessage: null,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
final message = e is ApiException ? e.message : '验证码发送失败,请重试';
|
||||
emit(state.copyWith(isSending: false, errorMessage: message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user