Search in sources :

Example 1 with PhoneNumberVerificationRequiredException

use of com.firebase.ui.auth.data.model.PhoneNumberVerificationRequiredException in project FirebaseUI-Android by firebase.

the class PhoneActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fui_activity_register_phone);
    final PhoneProviderResponseHandler handler = new ViewModelProvider(this).get(PhoneProviderResponseHandler.class);
    handler.init(getFlowParams());
    handler.getOperation().observe(this, new ResourceObserver<IdpResponse>(this, R.string.fui_progress_dialog_signing_in) {

        @Override
        protected void onSuccess(@NonNull IdpResponse response) {
            startSaveCredentials(handler.getCurrentUser(), response, null);
        }

        @Override
        protected void onFailure(@NonNull Exception e) {
            handleError(e);
        }
    });
    mPhoneVerifier = new ViewModelProvider(this).get(PhoneNumberVerificationHandler.class);
    mPhoneVerifier.init(getFlowParams());
    mPhoneVerifier.onRestoreInstanceState(savedInstanceState);
    mPhoneVerifier.getOperation().observe(this, new ResourceObserver<PhoneVerification>(this, R.string.fui_verifying) {

        @Override
        protected void onSuccess(@NonNull PhoneVerification verification) {
            if (verification.isAutoVerified()) {
                Toast.makeText(PhoneActivity.this, R.string.fui_auto_verified, Toast.LENGTH_LONG).show();
                FragmentManager manager = getSupportFragmentManager();
                if (manager.findFragmentByTag(SubmitConfirmationCodeFragment.TAG) != null) {
                    // Ensure the submit code screen isn't visible if there's no code to submit.
                    // It's possible to get into this state when an SMS is sent, but then
                    // automatically retrieved.
                    manager.popBackStack();
                }
            }
            handler.startSignIn(verification.getCredential(), new IdpResponse.Builder(new User.Builder(PhoneAuthProvider.PROVIDER_ID, null).setPhoneNumber(verification.getNumber()).build()).build());
        }

        @Override
        protected void onFailure(@NonNull Exception e) {
            if (e instanceof PhoneNumberVerificationRequiredException) {
                // already be visible so we have nothing to do.
                if (getSupportFragmentManager().findFragmentByTag(SubmitConfirmationCodeFragment.TAG) == null) {
                    showSubmitCodeFragment(((PhoneNumberVerificationRequiredException) e).getPhoneNumber());
                }
                // Clear existing errors
                handleError(null);
            } else {
                handleError(e);
            }
        }
    });
    if (savedInstanceState != null) {
        return;
    }
    Bundle params = getIntent().getExtras().getBundle(ExtraConstants.PARAMS);
    CheckPhoneNumberFragment fragment = CheckPhoneNumberFragment.newInstance(params);
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_phone, fragment, CheckPhoneNumberFragment.TAG).disallowAddToBackStack().commit();
}
Also used : Bundle(android.os.Bundle) PhoneNumberVerificationRequiredException(com.firebase.ui.auth.data.model.PhoneNumberVerificationRequiredException) PhoneNumberVerificationRequiredException(com.firebase.ui.auth.data.model.PhoneNumberVerificationRequiredException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) FragmentManager(androidx.fragment.app.FragmentManager) PhoneProviderResponseHandler(com.firebase.ui.auth.viewmodel.phone.PhoneProviderResponseHandler) ViewModelProvider(androidx.lifecycle.ViewModelProvider) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 2 with PhoneNumberVerificationRequiredException

use of com.firebase.ui.auth.data.model.PhoneNumberVerificationRequiredException in project FirebaseUI-Android by firebase.

the class PhoneNumberVerificationHandler method verifyPhoneNumber.

public void verifyPhoneNumber(@NonNull Activity activity, final String number, boolean force) {
    setResult(Resource.forLoading());
    PhoneAuthOptions.Builder optionsBuilder = PhoneAuthOptions.newBuilder(getAuth()).setPhoneNumber(number).setTimeout(AUTO_RETRIEVAL_TIMEOUT_SECONDS, TimeUnit.SECONDS).setActivity(activity).setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(@NonNull PhoneAuthCredential credential) {
            setResult(Resource.forSuccess(new PhoneVerification(number, credential, true)));
        }

        @Override
        public void onVerificationFailed(@NonNull FirebaseException e) {
            setResult(Resource.forFailure(e));
        }

        @Override
        public void onCodeSent(@NonNull String verificationId, @NonNull PhoneAuthProvider.ForceResendingToken token) {
            mVerificationId = verificationId;
            mForceResendingToken = token;
            setResult(Resource.forFailure(new PhoneNumberVerificationRequiredException(number)));
        }
    });
    if (force) {
        optionsBuilder.setForceResendingToken(mForceResendingToken);
    }
    PhoneAuthProvider.verifyPhoneNumber(optionsBuilder.build());
}
Also used : FirebaseException(com.google.firebase.FirebaseException) PhoneNumberVerificationRequiredException(com.firebase.ui.auth.data.model.PhoneNumberVerificationRequiredException) PhoneAuthCredential(com.google.firebase.auth.PhoneAuthCredential) PhoneAuthOptions(com.google.firebase.auth.PhoneAuthOptions) PhoneAuthProvider(com.google.firebase.auth.PhoneAuthProvider)

Aggregations

PhoneNumberVerificationRequiredException (com.firebase.ui.auth.data.model.PhoneNumberVerificationRequiredException)2 Bundle (android.os.Bundle)1 FragmentManager (androidx.fragment.app.FragmentManager)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)1 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)1 IdpResponse (com.firebase.ui.auth.IdpResponse)1 PhoneProviderResponseHandler (com.firebase.ui.auth.viewmodel.phone.PhoneProviderResponseHandler)1 FirebaseException (com.google.firebase.FirebaseException)1 FirebaseAuthException (com.google.firebase.auth.FirebaseAuthException)1 PhoneAuthCredential (com.google.firebase.auth.PhoneAuthCredential)1 PhoneAuthOptions (com.google.firebase.auth.PhoneAuthOptions)1 PhoneAuthProvider (com.google.firebase.auth.PhoneAuthProvider)1