-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add phone number authentication
- Configure native android files - Implement the logic with flutterbloc
- Loading branch information
Showing
55 changed files
with
3,801 additions
and
555 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
lib/authentication_with_phone_number/create_password/create_password_bloc.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:formz/formz.dart'; | ||
import 'package:fpb/authentication_with_firebase/application/bloc/auth_bloc.dart'; | ||
import 'package:fpb/authentication_with_firebase/domain/i_auth_facade.dart'; | ||
import 'package:fpb/core/failures/auth_failure.dart'; | ||
import 'package:fpb/sign_in/domain/domain.dart'; | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:injectable/injectable.dart'; | ||
|
||
part 'create_password_event.dart'; | ||
part 'create_password_state.dart'; | ||
part 'create_password_bloc.freezed.dart'; | ||
|
||
@injectable | ||
@LazySingleton() | ||
class CreatePasswordBloc extends Bloc<CreatePasswordEvent, CreatePasswordState> { | ||
CreatePasswordBloc(this._authenticationRepository, this.phoneNumber, this.authBloc) : super(const CreatePasswordState()) { | ||
on<_PasswordChanged>(_onPasswordChanged); | ||
on<_ConfirmPasswordChanged>(_onConfirmPasswordChanged); | ||
on<_Submitted>(_onSubmitted); | ||
} | ||
|
||
final IAuthFacade _authenticationRepository; | ||
String phoneNumber; | ||
final AuthBloc authBloc; | ||
|
||
void _onPasswordChanged( | ||
_PasswordChanged event, | ||
Emitter<CreatePasswordState> emit, | ||
) { | ||
final password = Password.dirty(event.password); | ||
emit( | ||
state.copyWith( | ||
password: password, | ||
status: Formz.validate([password]), failure: AuthFailure.fromErrorMessage('Oops!! something went wrong'), | ||
), | ||
); | ||
} | ||
|
||
void _onConfirmPasswordChanged( | ||
_ConfirmPasswordChanged event, | ||
Emitter<CreatePasswordState> emit, | ||
) { | ||
final confirmPassword = Password.dirty(event.password); | ||
FormzStatus status = Formz.validate([confirmPassword]);; | ||
if(status == FormzStatus.valid){ | ||
if(confirmPassword.value != state.password.value){ | ||
status = FormzStatus.invalid; | ||
} | ||
} | ||
emit( | ||
state.copyWith( | ||
confirmPassword: confirmPassword, | ||
status: status , failure: AuthFailure.fromErrorMessage('Oops!! something went wrong'), | ||
), | ||
); | ||
} | ||
|
||
Future<void> _onSubmitted( | ||
_Submitted event, | ||
Emitter<CreatePasswordState> emit, | ||
) async { | ||
if (state.status.isValidated) { | ||
emit(state.copyWith(status: FormzStatus.submissionInProgress)); | ||
final String emailDomain = '@flutterplaza.com'; | ||
String email = '$phoneNumber$emailDomain'; | ||
|
||
try { | ||
final failureOrUser = | ||
await _authenticationRepository.signUpWithEmailAndPassword(email: email, password: state.password.value); | ||
failureOrUser.fold( | ||
(failure) => | ||
emit(state.copyWith(status: FormzStatus.submissionFailure, failure: failure)), | ||
(user) { | ||
authBloc.add(AuthEvent.userChanged(user)); | ||
return emit(state.copyWith(status: FormzStatus.submissionSuccess)); | ||
} | ||
); | ||
} catch (_) { | ||
emit(state.copyWith(status: FormzStatus.submissionFailure, failure: AuthFailure.fromErrorMessage('Oops!! something went wrong'),),); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.