Skip to content

Commit

Permalink
feat: add phone number authentication
Browse files Browse the repository at this point in the history
- Configure native android files
- Implement the logic with flutterbloc
  • Loading branch information
lkmandy committed Mar 16, 2023
1 parent f839dcf commit 07febba
Show file tree
Hide file tree
Showing 55 changed files with 3,801 additions and 555 deletions.
3 changes: 2 additions & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ dependencies {
implementation platform('com.google.firebase:firebase-bom:31.2.0')
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.android.support:multidex:2.0.1'
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.google.android.gms:play-services-safetynet:18.0.1'

/// Facebook SDK
implementation 'com.facebook.android:facebook-android-sdk:latest.release'
Expand All @@ -128,7 +130,6 @@ dependencies {

// TODO: Add the dependencies for Firebase products you want to use
// When using the BoM, don't specify versions in Firebase dependencies
implementation 'com.google.firebase:firebase-analytics-ktx'

// Add the dependencies for any other desired Firebase products
// https://firebase.google.com/docs/android/setup#available-libraries
Expand Down
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'),),);
}
}
}
}
Loading

0 comments on commit 07febba

Please sign in to comment.