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();
}
}
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());
}
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();
}
}
}
});
}
}
}
}
}
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();
}
});
}
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)));
}
});
});
}
Aggregations