use of com.firebase.ui.auth.data.model.User in project FirebaseUI-Android by firebase.
the class ProfileMerger method then.
@Override
public Task<AuthResult> then(@NonNull Task<AuthResult> task) {
final AuthResult authResult = task.getResult();
FirebaseUser firebaseUser = authResult.getUser();
String name = firebaseUser.getDisplayName();
Uri photoUri = firebaseUser.getPhotoUrl();
if (!TextUtils.isEmpty(name) && photoUri != null) {
return Tasks.forResult(authResult);
}
User user = mResponse.getUser();
if (TextUtils.isEmpty(name)) {
name = user.getName();
}
if (photoUri == null) {
photoUri = user.getPhotoUri();
}
return firebaseUser.updateProfile(new UserProfileChangeRequest.Builder().setDisplayName(name).setPhotoUri(photoUri).build()).addOnFailureListener(new TaskFailureLogger(TAG, "Error updating profile")).continueWithTask(task1 -> Tasks.forResult(authResult));
}
use of com.firebase.ui.auth.data.model.User in project FirebaseUI-Android by firebase.
the class SingleSignInActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
User user = User.getUser(getIntent());
final String provider = user.getProviderId();
AuthUI.IdpConfig providerConfig = ProviderUtils.getConfigFromIdps(getFlowParams().providers, provider);
if (providerConfig == null) {
finish(RESULT_CANCELED, IdpResponse.getErrorIntent(new FirebaseUiException(ErrorCodes.DEVELOPER_ERROR, "Provider not enabled: " + provider)));
return;
}
ViewModelProvider supplier = new ViewModelProvider(this);
mHandler = supplier.get(SocialProviderResponseHandler.class);
mHandler.init(getFlowParams());
boolean useEmulator = getAuthUI().isUseEmulator();
switch(provider) {
case GoogleAuthProvider.PROVIDER_ID:
if (useEmulator) {
mProvider = supplier.get(GenericIdpSignInHandler.class).initWith(GenericIdpSignInHandler.getGenericGoogleConfig());
} else {
mProvider = supplier.get(GoogleSignInHandler.class).initWith(new GoogleSignInHandler.Params(providerConfig, user.getEmail()));
}
break;
case FacebookAuthProvider.PROVIDER_ID:
if (useEmulator) {
mProvider = supplier.get(GenericIdpSignInHandler.class).initWith(GenericIdpSignInHandler.getGenericFacebookConfig());
} else {
mProvider = supplier.get(FacebookSignInHandler.class).initWith(providerConfig);
}
break;
default:
if (!TextUtils.isEmpty(providerConfig.getParams().getString(GENERIC_OAUTH_PROVIDER_ID))) {
mProvider = supplier.get(GenericIdpSignInHandler.class).initWith(providerConfig);
break;
}
throw new IllegalStateException("Invalid provider id: " + provider);
}
mProvider.getOperation().observe(this, new ResourceObserver<IdpResponse>(this) {
@Override
protected void onSuccess(@NonNull IdpResponse response) {
boolean useSocialHandler = AuthUI.SOCIAL_PROVIDERS.contains(provider) && !getAuthUI().isUseEmulator();
if (useSocialHandler || !response.isSuccessful()) {
mHandler.startSignIn(response);
return;
}
finish(response.isSuccessful() ? RESULT_OK : RESULT_CANCELED, response.toIntent());
}
@Override
protected void onFailure(@NonNull Exception e) {
if (e instanceof FirebaseAuthAnonymousUpgradeException) {
finish(RESULT_CANCELED, new Intent().putExtra(ExtraConstants.IDP_RESPONSE, IdpResponse.from(e)));
return;
}
mHandler.startSignIn(IdpResponse.from(e));
}
});
mHandler.getOperation().observe(this, new ResourceObserver<IdpResponse>(this) {
@Override
protected void onSuccess(@NonNull IdpResponse response) {
startSaveCredentials(mHandler.getCurrentUser(), response, null);
}
@Override
protected void onFailure(@NonNull Exception e) {
if (e instanceof FirebaseAuthAnonymousUpgradeException) {
IdpResponse res = ((FirebaseAuthAnonymousUpgradeException) e).getResponse();
finish(RESULT_CANCELED, new Intent().putExtra(ExtraConstants.IDP_RESPONSE, res));
} else {
finish(RESULT_CANCELED, IdpResponse.getErrorIntent(e));
}
}
});
if (mHandler.getOperation().getValue() == null) {
mProvider.startSignIn(getAuth(), this, provider);
}
}
use of com.firebase.ui.auth.data.model.User 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)));
}
});
});
}
use of com.firebase.ui.auth.data.model.User in project FirebaseUI-Android by firebase.
the class WelcomeBackPasswordHandlerTest method testSignIn_anonymousUserUpgradeEnabledWithSocialFlow_expectSafeLinksIdpCredentialAndMergeFailure.
@Test
public void testSignIn_anonymousUserUpgradeEnabledWithSocialFlow_expectSafeLinksIdpCredentialAndMergeFailure() {
setupAnonymousUpgrade();
mHandler.getOperation().observeForever(mResponseObserver);
// Fake social response from Facebook
User user = new User.Builder(FacebookAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build();
IdpResponse response = new IdpResponse.Builder(user).setToken(TestConstants.TOKEN).setSecret(TestConstants.SECRET).build();
AuthCredential credential = FacebookAuthProvider.getCredential(TestConstants.TOKEN);
// Need to control FirebaseAuth's return values
AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
authOperationManager.mScratchAuth = mScratchMockAuth;
when(mScratchMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));
// Mock linking with Facebook to always work
when(FakeAuthResult.INSTANCE.getUser().linkWithCredential(credential)).thenReturn(new AutoContinueTask<>(FakeAuthResult.INSTANCE, FakeAuthResult.INSTANCE, true, null));
// Kick off the sign in flow
mHandler.startSignIn(TestConstants.EMAIL, TestConstants.PASSWORD, response, credential);
// Verify that signInWithCredential was called
ArgumentCaptor<EmailAuthCredential> credentialCaptor = ArgumentCaptor.forClass(EmailAuthCredential.class);
verify(mScratchMockAuth).signInWithCredential(credentialCaptor.capture());
EmailAuthCredential capturedCredential = credentialCaptor.getValue();
// TODO: EmailAuthCredential no longer exposes .getEmail() or .getPassword()
// assertThat(capturedCredential.getEmail()).isEqualTo(TestConstants.EMAIL);
// assertThat(capturedCredential.getPassword()).isEqualTo(TestConstants.PASSWORD);
// Verify that account linking is attempted
verify(FakeAuthResult.INSTANCE.getUser()).linkWithCredential(credential);
verifyMergeFailure();
}
use of com.firebase.ui.auth.data.model.User in project FirebaseUI-Android by firebase.
the class WelcomeBackPasswordHandlerTest method testSignIn_anonymousUserUpgradeEnabledWithEmailFlow_expectValidateCredentialAndMergeFailure.
@Test
public void testSignIn_anonymousUserUpgradeEnabledWithEmailFlow_expectValidateCredentialAndMergeFailure() {
setupAnonymousUpgrade();
mHandler.getOperation().observeForever(mResponseObserver);
// Fake user with password provider
User user = new User.Builder(EmailAuthProvider.PROVIDER_ID, TestConstants.EMAIL).build();
IdpResponse response = new IdpResponse.Builder(user).build();
// Need to control FirebaseAuth's return values
AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
authOperationManager.mScratchAuth = mScratchMockAuth;
when(mScratchMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoCompleteTask.forSuccess(FakeAuthResult.INSTANCE));
// Kick off the sign in flow
mHandler.startSignIn(TestConstants.EMAIL, TestConstants.PASSWORD, response, null);
// Verify that signInWithCredential was called
ArgumentCaptor<EmailAuthCredential> credentialCaptor = ArgumentCaptor.forClass(EmailAuthCredential.class);
verify(mScratchMockAuth).signInWithCredential(credentialCaptor.capture());
EmailAuthCredential capturedCredential = credentialCaptor.getValue();
// TODO: EmailAuthCredential no longer exposes .getEmail() or .getPassword()
// assertThat(capturedCredential.getEmail()).isEqualTo(TestConstants.EMAIL);
// assertThat(capturedCredential.getPassword()).isEqualTo(TestConstants.PASSWORD);
verifyMergeFailure();
}
Aggregations