Search in sources :

Example 1 with FirebaseAuthUserCollisionException

use of com.google.firebase.auth.FirebaseAuthUserCollisionException in project FirebaseUI-Android by firebase.

the class CredentialSignInHandler method onComplete.

@Override
public void onComplete(@NonNull Task<AuthResult> task) {
    if (task.isSuccessful()) {
        FirebaseUser firebaseUser = task.getResult().getUser();
        mHelper.saveCredentialsOrFinish(mSmartLock, mActivity, firebaseUser, mResponse);
    } else {
        if (task.getException() instanceof FirebaseAuthUserCollisionException) {
            final String email = mResponse.getEmail();
            if (email != null) {
                mHelper.getFirebaseAuth().fetchProvidersForEmail(email).addOnFailureListener(new TaskFailureLogger(TAG, "Error fetching providers for email")).addOnSuccessListener(new StartWelcomeBackFlow()).addOnFailureListener(new OnFailureListener() {

                    @Override
                    public void onFailure(@NonNull Exception e) {
                    // TODO: What to do when signing in with Credential fails
                    // and we can't continue to Welcome back flow without
                    // knowing providers?
                    }
                });
                return;
            }
        } else {
            Log.e(TAG, "Unexpected exception when signing in with credential " + mResponse.getProviderType() + " unsuccessful. Visit https://console.firebase.google.com to enable it.", task.getException());
        }
        mHelper.dismissDialog();
    }
}
Also used : TaskFailureLogger(com.firebase.ui.auth.ui.TaskFailureLogger) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUser(com.google.firebase.auth.FirebaseUser) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException)

Example 2 with FirebaseAuthUserCollisionException

use of com.google.firebase.auth.FirebaseAuthUserCollisionException in project FirebaseUI-Android by firebase.

the class CredentialSignInHandlerTest method testSignInFailed_withFacebookAlreadyLinked.

@Test
public void testSignInFailed_withFacebookAlreadyLinked() {
    AppCompatBase mockActivity = mock(AppCompatBase.class);
    ActivityHelper mockActivityHelper = mock(ActivityHelper.class);
    FirebaseAuth mockFirebaseAuth = mock(FirebaseAuth.class);
    IdpResponse idpResponse = new IdpResponse(GoogleAuthProvider.PROVIDER_ID, TestConstants.EMAIL);
    CredentialSignInHandler credentialSignInHandler = new CredentialSignInHandler(mockActivity, mockActivityHelper, null, RC_ACCOUNT_LINK, idpResponse);
    FlowParameters mockFlowParams = mock(FlowParameters.class);
    when(mockActivityHelper.getFirebaseAuth()).thenReturn(mockFirebaseAuth);
    when(mockActivityHelper.getFlowParams()).thenReturn(mockFlowParams);
    // pretend the account has Facebook linked already
    when(mockFirebaseAuth.fetchProvidersForEmail(TestConstants.EMAIL)).thenReturn(new AutoCompleteTask<ProviderQueryResult>(new FakeProviderQueryResult(Arrays.asList(FacebookAuthProvider.PROVIDER_ID)), true, null));
    // pretend there was already an account with this email
    Task exceptionTask = Tasks.forException(new FirebaseAuthUserCollisionException(LINKING_ERROR, LINKING_EXPLANATION));
    credentialSignInHandler.onComplete(exceptionTask);
    ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
    ArgumentCaptor<Integer> intCaptor = ArgumentCaptor.forClass(Integer.class);
    verify(mockActivity).startActivityForResult(intentCaptor.capture(), intCaptor.capture());
    Intent capturedIntent = intentCaptor.getValue();
    User capturedUser = User.getUser(capturedIntent);
    assertEquals(RC_ACCOUNT_LINK, (int) intCaptor.getValue());
    assertEquals(WelcomeBackIdpPrompt.class.getName(), capturedIntent.getComponent().getClassName());
    assertEquals(TestConstants.EMAIL, capturedUser.getEmail());
    assertEquals(FacebookAuthProvider.PROVIDER_ID, capturedUser.getProvider());
}
Also used : Task(com.google.android.gms.tasks.Task) AutoCompleteTask(com.firebase.ui.auth.testhelpers.AutoCompleteTask) FirebaseUser(com.google.firebase.auth.FirebaseUser) User(com.firebase.ui.auth.ui.User) AppCompatBase(com.firebase.ui.auth.ui.AppCompatBase) Intent(android.content.Intent) FakeProviderQueryResult(com.firebase.ui.auth.testhelpers.FakeProviderQueryResult) ProviderQueryResult(com.google.firebase.auth.ProviderQueryResult) ActivityHelper(com.firebase.ui.auth.ui.ActivityHelper) FlowParameters(com.firebase.ui.auth.ui.FlowParameters) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) WelcomeBackIdpPrompt(com.firebase.ui.auth.ui.accountlink.WelcomeBackIdpPrompt) FakeProviderQueryResult(com.firebase.ui.auth.testhelpers.FakeProviderQueryResult) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) IdpResponse(com.firebase.ui.auth.IdpResponse) Test(org.junit.Test)

Example 3 with FirebaseAuthUserCollisionException

use of com.google.firebase.auth.FirebaseAuthUserCollisionException in project BloodHub by kazijehangir.

the class OrganizationRegistrationActivity method registerNewOrg.

private void registerNewOrg() {
    AutoCompleteTextView mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
    EditText mPasswordView = (EditText) findViewById(R.id.password);
    CheckBox mTermsAgree = (CheckBox) findViewById(R.id.agreeTerms);
    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
    String email = mEmailView.getText().toString();
    String password = mPasswordView.getText().toString();
    if (!email.contains("@")) {
        Toast.makeText(this, "Not a valid email address", Toast.LENGTH_SHORT).show();
    } else {
        if (password.length() < 4) {
            Toast.makeText(this, "Password is too short, minimum length is 4.", Toast.LENGTH_SHORT).show();
        } else {
            if (email.contains(":") || password.contains(":")) {
                Toast.makeText(this, "Email address and Password cannot contain ':'", Toast.LENGTH_SHORT).show();
            } else {
                if (!mTermsAgree.isChecked()) {
                    Toast.makeText(this, "You need to agree to share your details.", Toast.LENGTH_SHORT).show();
                } else {
                    progressBar.setVisibility(View.VISIBLE);
                    mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {

                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            Context context = getApplicationContext();
                            progressBar.setVisibility(View.GONE);
                            if (task.isSuccessful()) {
                                FirebaseUser user = mAuth.getCurrentUser();
                                sendVerificationEmail();
                                // take user to main screen
                                progressBar.setVisibility(View.GONE);
                                Intent intent = new Intent(context, LoginActivity.class);
                                startActivity(intent);
                            } else {
                                if (task.getException() instanceof FirebaseAuthUserCollisionException) {
                                    Toast.makeText(context, "User with this email already exists.", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(context, "Error creating user", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    });
                }
            }
        }
    }
}
Also used : EditText(android.widget.EditText) Context(android.content.Context) AuthResult(com.google.firebase.auth.AuthResult) Intent(android.content.Intent) FirebaseUser(com.google.firebase.auth.FirebaseUser) CheckBox(android.widget.CheckBox) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) ProgressBar(android.widget.ProgressBar) AutoCompleteTextView(android.widget.AutoCompleteTextView)

Example 4 with FirebaseAuthUserCollisionException

use of com.google.firebase.auth.FirebaseAuthUserCollisionException in project Firebase-Helper by AtifAbbAsi19.

the class FireBaseHelper method registerUser.

private void registerUser(final String email, String password, Context context) {
    // if (TextUtils.isEmpty(phone_number)) {
    // Toast.makeText(this, "Please enter Number", Toast.LENGTH_LONG).show();
    // return;
    // }
    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
    // if the email and password are not empty
    // displaying a progress dialog
    // progressDialog.setMessage("Registering Please Wait...");
    // progressDialog.show();
    // /http://stackoverflow.com/questions/40404567/how-to-send-verification-email-with-firebase
    // .addOnCompleteListener((Activity) context, new OnCompleteListener<AuthResult>() {
    // .addOnCompleteListener((Executor) this, new OnCompleteListener<AuthResult>() {
    // creating a new user
    firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener((Activity) context, new OnCompleteListener<AuthResult>() {

        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            // checking if success
            if (task.isSuccessful()) {
                // display some message here
                String userId = task.getResult().getUser().getUid();
            // Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            // startActivity(intent);
            // https://firebase.googleblog.com/2017/02/email-verification-in-firebase-auth.html
            // startActivity(new Intent(Signup_Activity.this, MainHomeDashBoard.class));
            // Toast.makeText(getApplicationContext(), "Successfully registered", Toast.LENGTH_LONG).show();
            } else {
                if (task.getException() instanceof FirebaseAuthUserCollisionException) {
                // Toast.makeText(Signup.this, "User with this email already exist.", Toast.LENGTH_SHORT).show();
                }
            // 
            // //display some message here
            // Toast.makeText(getApplicationContext(), "Registration Error", Toast.LENGTH_LONG).show();
            }
        // progressDialog.dismiss();
        }
    });
}
Also used : AuthResult(com.google.firebase.auth.AuthResult) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuth(com.google.firebase.auth.FirebaseAuth)

Example 5 with FirebaseAuthUserCollisionException

use of com.google.firebase.auth.FirebaseAuthUserCollisionException in project FirebaseUI-Android by firebase.

the class GenericIdpSignInHandler method handleAnonymousUpgradeFlow.

private void handleAnonymousUpgradeFlow(final FirebaseAuth auth, final HelperActivityBase activity, final OAuthProvider provider, final FlowParameters flowParameters) {
    final boolean useEmulator = activity.getAuthUI().isUseEmulator();
    auth.getCurrentUser().startActivityForLinkWithProvider(activity, provider).addOnSuccessListener(authResult -> handleSuccess(useEmulator, provider.getProviderId(), authResult.getUser(), (OAuthCredential) authResult.getCredential(), authResult.getAdditionalUserInfo().isNewUser())).addOnFailureListener(e -> {
        if (!(e instanceof FirebaseAuthUserCollisionException)) {
            setResult(Resource.forFailure(e));
            return;
        }
        FirebaseAuthUserCollisionException collisionException = (FirebaseAuthUserCollisionException) e;
        final AuthCredential credential = collisionException.getUpdatedCredential();
        final String email = collisionException.getEmail();
        // Case 1: Anonymous user trying to link with an existing user
        // Case 2: Anonymous user trying to link with a provider keyed
        // by an email that already belongs to an existing account
        // (linking flow)
        ProviderUtils.fetchSortedProviders(auth, flowParameters, email).addOnSuccessListener(providers -> {
            if (providers.isEmpty()) {
                String errorMessage = "Unable to complete the linkingflow -" + " the user is using " + "unsupported providers.";
                setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.DEVELOPER_ERROR, errorMessage)));
                return;
            }
            if (providers.contains(provider.getProviderId())) {
                // Case 1
                handleMergeFailure(credential);
            } else {
                // Case 2 - linking flow to be handled by
                // SocialProviderResponseHandler
                setResult(Resource.forFailure(new FirebaseUiUserCollisionException(ErrorCodes.ERROR_GENERIC_IDP_RECOVERABLE_ERROR, "Recoverable error.", provider.getProviderId(), email, credential)));
            }
        });
    });
}
Also used : ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) NonNull(androidx.annotation.NonNull) User(com.firebase.ui.auth.data.model.User) Intent(android.content.Intent) Resource(com.firebase.ui.auth.data.model.Resource) ExtraConstants(com.firebase.ui.auth.util.ExtraConstants) HashMap(java.util.HashMap) FirebaseUiUserCollisionException(com.firebase.ui.auth.FirebaseUiUserCollisionException) FirebaseAuthAnonymousUpgradeException(com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException) GoogleAuthProvider(com.google.firebase.auth.GoogleAuthProvider) OAuthCredential(com.google.firebase.auth.OAuthCredential) RestrictTo(androidx.annotation.RestrictTo) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) AuthUI(com.firebase.ui.auth.AuthUI) R(com.firebase.ui.auth.R) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) UserCancellationException(com.firebase.ui.auth.data.model.UserCancellationException) HelperActivityBase(com.firebase.ui.auth.ui.HelperActivityBase) FirebaseUser(com.google.firebase.auth.FirebaseUser) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) FacebookAuthProvider(com.google.firebase.auth.FacebookAuthProvider) FirebaseAuthError(com.firebase.ui.auth.util.FirebaseAuthError) OAuthProvider(com.google.firebase.auth.OAuthProvider) AuthCredential(com.google.firebase.auth.AuthCredential) FlowParameters(com.firebase.ui.auth.data.model.FlowParameters) List(java.util.List) Nullable(androidx.annotation.Nullable) Application(android.app.Application) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) FirebaseAuthException(com.google.firebase.auth.FirebaseAuthException) ProviderSignInBase(com.firebase.ui.auth.viewmodel.ProviderSignInBase) RequestCodes(com.firebase.ui.auth.viewmodel.RequestCodes) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) VisibleForTesting(androidx.annotation.VisibleForTesting) IdpResponse(com.firebase.ui.auth.IdpResponse) OAuthCredential(com.google.firebase.auth.OAuthCredential) AuthCredential(com.google.firebase.auth.AuthCredential) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) FirebaseUiUserCollisionException(com.firebase.ui.auth.FirebaseUiUserCollisionException)

Aggregations

FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)22 IdpResponse (com.firebase.ui.auth.IdpResponse)15 Resource (com.firebase.ui.auth.data.model.Resource)15 AuthCredential (com.google.firebase.auth.AuthCredential)14 Test (org.junit.Test)12 FirebaseUser (com.google.firebase.auth.FirebaseUser)10 InOrder (org.mockito.InOrder)10 AuthResult (com.google.firebase.auth.AuthResult)9 Intent (android.content.Intent)7 User (com.firebase.ui.auth.data.model.User)7 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)7 FirebaseAuthAnonymousUpgradeException (com.firebase.ui.auth.FirebaseAuthAnonymousUpgradeException)6 FakeSignInMethodQueryResult (com.firebase.ui.auth.testhelpers.FakeSignInMethodQueryResult)6 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)6 Application (android.app.Application)5 NonNull (androidx.annotation.NonNull)5 Nullable (androidx.annotation.Nullable)5 ErrorCodes (com.firebase.ui.auth.ErrorCodes)5 FirebaseUiException (com.firebase.ui.auth.FirebaseUiException)5 IntentRequiredException (com.firebase.ui.auth.data.model.IntentRequiredException)5