Search in sources :

Example 6 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.

the class RegisterEmailFragment method registerUser.

private void registerUser(final String email, final String name, final String password) {
    mHelper.getFirebaseAuth().createUserWithEmailAndPassword(email, password).addOnFailureListener(new TaskFailureLogger(TAG, "Error creating user")).addOnSuccessListener(new OnSuccessListener<AuthResult>() {

        @Override
        public void onSuccess(AuthResult authResult) {
            // Set display name
            UserProfileChangeRequest changeNameRequest = new UserProfileChangeRequest.Builder().setDisplayName(name).setPhotoUri(mUser.getPhotoUri()).build();
            final FirebaseUser user = authResult.getUser();
            user.updateProfile(changeNameRequest).addOnFailureListener(new TaskFailureLogger(TAG, "Error setting display name")).addOnCompleteListener(new OnCompleteListener<Void>() {

                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    // This executes even if the name change fails, since
                    // the account creation succeeded and we want to save
                    // the credential to SmartLock (if enabled).
                    mHelper.saveCredentialsOrFinish(mSaveSmartLock, getActivity(), user, password, new IdpResponse(EmailAuthProvider.PROVIDER_ID, email));
                }
            });
        }
    }).addOnFailureListener(getActivity(), new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception e) {
            mHelper.dismissDialog();
            if (e instanceof FirebaseAuthWeakPasswordException) {
                // Password too weak
                mPasswordInput.setError(getResources().getQuantityString(R.plurals.error_weak_password, R.integer.min_password_length));
            } else if (e instanceof FirebaseAuthInvalidCredentialsException) {
                // Email address is malformed
                mEmailInput.setError(getString(R.string.invalid_email_address));
            } else if (e instanceof FirebaseAuthUserCollisionException) {
                // Collision with existing user email, it should be very hard for
                // the user to even get to this error due to CheckEmailFragment.
                mEmailInput.setError(getString(R.string.error_user_collision));
            } else {
                // General error message, this branch should not be invoked but
                // covers future API changes
                mEmailInput.setError(getString(R.string.email_account_creation_error));
            }
        }
    });
}
Also used : TaskFailureLogger(com.firebase.ui.auth.ui.TaskFailureLogger) FirebaseAuthWeakPasswordException(com.google.firebase.auth.FirebaseAuthWeakPasswordException) UserProfileChangeRequest(com.google.firebase.auth.UserProfileChangeRequest) AuthResult(com.google.firebase.auth.AuthResult) FirebaseUser(com.google.firebase.auth.FirebaseUser) FirebaseAuthWeakPasswordException(com.google.firebase.auth.FirebaseAuthWeakPasswordException) FirebaseAuthInvalidCredentialsException(com.google.firebase.auth.FirebaseAuthInvalidCredentialsException) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseAuthInvalidCredentialsException(com.google.firebase.auth.FirebaseAuthInvalidCredentialsException) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 7 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.

the class WelcomeBackPasswordPrompt method next.

private void next(final String email, final String password) {
    // Check for null or empty password
    if (TextUtils.isEmpty(password)) {
        mPasswordLayout.setError(getString(R.string.required_field));
        return;
    } else {
        mPasswordLayout.setError(null);
    }
    mActivityHelper.showLoadingDialog(R.string.progress_dialog_signing_in);
    final FirebaseAuth firebaseAuth = mActivityHelper.getFirebaseAuth();
    // Sign in with known email and the password provided
    firebaseAuth.signInWithEmailAndPassword(email, password).addOnFailureListener(new TaskFailureLogger(TAG, "Error signing in with email and password")).addOnSuccessListener(new OnSuccessListener<AuthResult>() {

        @Override
        public void onSuccess(AuthResult authResult) {
            AuthCredential authCredential = AuthCredentialHelper.getAuthCredential(mIdpResponse);
            // Otherwise, the user has an email account that we need to link to an idp.
            if (authCredential == null) {
                mActivityHelper.saveCredentialsOrFinish(mSaveSmartLock, authResult.getUser(), password, new IdpResponse(EmailAuthProvider.PROVIDER_ID, email));
            } else {
                authResult.getUser().linkWithCredential(authCredential).addOnFailureListener(new TaskFailureLogger(TAG, "Error signing in with credential " + authCredential.getProvider())).addOnSuccessListener(new OnSuccessListener<AuthResult>() {

                    @Override
                    public void onSuccess(AuthResult authResult) {
                        mActivityHelper.saveCredentialsOrFinish(mSaveSmartLock, authResult.getUser(), mIdpResponse);
                    }
                });
            }
        }
    }).addOnFailureListener(this, new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception e) {
            mActivityHelper.dismissDialog();
            String error = e.getLocalizedMessage();
            mPasswordLayout.setError(error);
        }
    });
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) TaskFailureLogger(com.firebase.ui.auth.ui.TaskFailureLogger) AuthResult(com.google.firebase.auth.AuthResult) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 8 with IdpResponse

use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.

the class CredentialSignInHandlerTest method testSignInFailed_withPasswordAccountAlreadyLinked.

@Test
public void testSignInFailed_withPasswordAccountAlreadyLinked() {
    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);
    Task mockTask = mock(Task.class);
    FlowParameters mockFlowParams = mock(FlowParameters.class);
    // pretend there was already an account with this email
    when(mockTask.getException()).thenReturn(new FirebaseAuthUserCollisionException(LINKING_ERROR, LINKING_EXPLANATION));
    when(mockActivityHelper.getFirebaseAuth()).thenReturn(mockFirebaseAuth);
    when(mockActivityHelper.getFlowParams()).thenReturn(mockFlowParams);
    // pretend the account has a Password account linked already
    when(mockFirebaseAuth.fetchProvidersForEmail(TestConstants.EMAIL)).thenReturn(new AutoCompleteTask<ProviderQueryResult>(new FakeProviderQueryResult(Arrays.asList(EmailAuthProvider.PROVIDER_ID)), true, null));
    credentialSignInHandler.onComplete(mockTask);
    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();
    assertEquals(RC_ACCOUNT_LINK, (int) intCaptor.getValue());
    assertEquals(WelcomeBackPasswordPrompt.class.getName(), capturedIntent.getComponent().getClassName());
    assertEquals(TestConstants.EMAIL, IdpResponse.fromResultIntent(capturedIntent).getEmail());
}
Also used : Task(com.google.android.gms.tasks.Task) AutoCompleteTask(com.firebase.ui.auth.testhelpers.AutoCompleteTask) 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) FakeProviderQueryResult(com.firebase.ui.auth.testhelpers.FakeProviderQueryResult) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) IdpResponse(com.firebase.ui.auth.IdpResponse) WelcomeBackPasswordPrompt(com.firebase.ui.auth.ui.accountlink.WelcomeBackPasswordPrompt) Test(org.junit.Test)

Aggregations

IdpResponse (com.firebase.ui.auth.IdpResponse)8 FirebaseUser (com.google.firebase.auth.FirebaseUser)4 Intent (android.content.Intent)3 AutoCompleteTask (com.firebase.ui.auth.testhelpers.AutoCompleteTask)3 ActivityHelper (com.firebase.ui.auth.ui.ActivityHelper)3 AppCompatBase (com.firebase.ui.auth.ui.AppCompatBase)3 Task (com.google.android.gms.tasks.Task)3 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)3 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)3 Test (org.junit.Test)3 FakeProviderQueryResult (com.firebase.ui.auth.testhelpers.FakeProviderQueryResult)2 FlowParameters (com.firebase.ui.auth.ui.FlowParameters)2 TaskFailureLogger (com.firebase.ui.auth.ui.TaskFailureLogger)2 User (com.firebase.ui.auth.ui.User)2 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)2 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)2 AuthResult (com.google.firebase.auth.AuthResult)2 ProviderQueryResult (com.google.firebase.auth.ProviderQueryResult)2 Activity (android.app.Activity)1 MainThread (android.support.annotation.MainThread)1