use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.
the class EmailProviderResponseHandler method startSignIn.
public void startSignIn(@NonNull final IdpResponse response, @NonNull final String password) {
if (!response.isSuccessful()) {
setResult(Resource.forFailure(response.getError()));
return;
}
if (!response.getProviderType().equals(EmailAuthProvider.PROVIDER_ID)) {
throw new IllegalStateException("This handler can only be used with the email provider");
}
setResult(Resource.forLoading());
final AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
final String email = response.getEmail();
authOperationManager.createOrLinkUserWithEmailAndPassword(getAuth(), getArguments(), email, password).continueWithTask(new ProfileMerger(response)).addOnFailureListener(new TaskFailureLogger(TAG, "Error creating user")).addOnSuccessListener(result -> handleSuccess(response, result)).addOnFailureListener(e -> {
if (e instanceof FirebaseAuthUserCollisionException) {
if (authOperationManager.canUpgradeAnonymous(getAuth(), getArguments())) {
AuthCredential credential = EmailAuthProvider.getCredential(email, password);
handleMergeFailure(credential);
} else {
Log.w(TAG, "Got a collision error during a non-upgrade flow", e);
// Collision with existing user email without anonymous upgrade
// it should be very hard for the user to even get to this error
// due to CheckEmailFragment.
ProviderUtils.fetchTopProvider(getAuth(), getArguments(), email).addOnSuccessListener(new StartWelcomeBackFlow(email)).addOnFailureListener(e1 -> setResult(Resource.forFailure(e1)));
}
} else {
setResult(Resource.forFailure(e));
}
});
}
use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.
the class SocialProviderResponseHandler method startSignIn.
public void startSignIn(@NonNull final IdpResponse response) {
if (!response.isSuccessful() && !response.isRecoverableErrorResponse()) {
setResult(Resource.forFailure(response.getError()));
return;
}
if (isEmailOrPhoneProvider(response.getProviderType())) {
throw new IllegalStateException("This handler cannot be used with email or phone providers");
}
setResult(Resource.forLoading());
// a credential made from the id token/access token cannot be used to sign-in.
if (response.hasCredentialForLinking()) {
handleGenericIdpLinkingFlow(response);
return;
}
final AuthCredential credential = ProviderUtils.getAuthCredential(response);
AuthOperationManager.getInstance().signInAndLinkWithCredential(getAuth(), getArguments(), credential).continueWithTask(new ProfileMerger(response)).addOnSuccessListener(result -> handleSuccess(response, result)).addOnFailureListener(e -> {
// For some reason disabled users can hit FirebaseAuthUserCollisionException
// so we have to handle this special case.
boolean isDisabledUser = (e instanceof FirebaseAuthInvalidUserException);
if (e instanceof FirebaseAuthException) {
FirebaseAuthException authEx = (FirebaseAuthException) e;
FirebaseAuthError fae = FirebaseAuthError.fromException(authEx);
if (fae == FirebaseAuthError.ERROR_USER_DISABLED) {
isDisabledUser = true;
}
}
if (isDisabledUser) {
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.ERROR_USER_DISABLED)));
} else if (e instanceof FirebaseAuthUserCollisionException) {
final String email = response.getEmail();
if (email == null) {
setResult(Resource.forFailure(e));
return;
}
// There can be a collision due to:
// CASE 1: Anon user signing in with a credential that belongs to an
// existing user.
// CASE 2: non - anon user signing in with a credential that does not
// belong to an existing user, but the email matches an existing user
// that has another social IDP. We need to link this new IDP to this
// existing user.
// CASE 3: CASE 2 with an anonymous user. We link the new IDP to the
// same account before handling invoking a merge failure.
ProviderUtils.fetchSortedProviders(getAuth(), getArguments(), email).addOnSuccessListener(providers -> {
if (providers.contains(response.getProviderType())) {
// Case 1
handleMergeFailure(credential);
} else if (providers.isEmpty()) {
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.DEVELOPER_ERROR, "No supported providers.")));
} else {
// Case 2 & 3 - we need to link
startWelcomeBackFlowForLinking(providers.get(0), response);
}
}).addOnFailureListener(e1 -> setResult(Resource.forFailure(e1)));
}
});
}
use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.
the class SocialProviderResponseHandler method onActivityResult.
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == RequestCodes.ACCOUNT_LINK_FLOW) {
IdpResponse response = IdpResponse.fromResultIntent(data);
if (resultCode == Activity.RESULT_OK) {
setResult(Resource.forSuccess(response));
} else {
Exception e;
if (response == null) {
e = new FirebaseUiException(ErrorCodes.UNKNOWN_ERROR, "Link canceled by user.");
} else {
e = response.getError();
}
setResult(Resource.forFailure(e));
}
}
}
use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.
the class EmailLinkPersistanceManagerTest method testSaveAndRetrieveIdpResonseForLinking_noSavedEmail_expectNothingSaved.
@Test
public void testSaveAndRetrieveIdpResonseForLinking_noSavedEmail_expectNothingSaved() {
IdpResponse response = buildIdpResponse();
mPersistenceManager.saveIdpResponseForLinking(ApplicationProvider.getApplicationContext(), response);
SessionRecord sessionRecord = mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext());
assertThat(sessionRecord).isNull();
}
use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.
the class EmailLinkSignInHandler method handleLinkingFlow.
private void handleLinkingFlow(final AuthOperationManager authOperationManager, final EmailLinkPersistenceManager persistenceManager, final IdpResponse response, final String link) {
final AuthCredential storedCredentialForLink = ProviderUtils.getAuthCredential(response);
final AuthCredential emailLinkCredential = EmailAuthProvider.getCredentialWithLink(response.getEmail(), link);
if (authOperationManager.canUpgradeAnonymous(getAuth(), getArguments())) {
authOperationManager.safeLink(emailLinkCredential, storedCredentialForLink, getArguments()).addOnCompleteListener(task -> {
persistenceManager.clearAllData(getApplication());
if (task.isSuccessful()) {
handleMergeFailure(storedCredentialForLink);
} else {
setResult(Resource.forFailure(task.getException()));
}
});
} else {
getAuth().signInWithCredential(emailLinkCredential).continueWithTask(task -> {
persistenceManager.clearAllData(getApplication());
if (!task.isSuccessful()) {
return task;
}
return task.getResult().getUser().linkWithCredential(storedCredentialForLink).continueWithTask(new ProfileMerger(response)).addOnFailureListener(new TaskFailureLogger(TAG, "linkWithCredential+merge failed."));
}).addOnSuccessListener(authResult -> {
FirebaseUser user = authResult.getUser();
IdpResponse response1 = new IdpResponse.Builder(new User.Builder(EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD, user.getEmail()).setName(user.getDisplayName()).setPhotoUri(user.getPhotoUrl()).build()).build();
handleSuccess(response1, authResult);
}).addOnFailureListener(e -> setResult(Resource.forFailure(e)));
}
}
Aggregations