Search in sources :

Example 51 with FirebaseUser

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

the class WelcomeBackPasswordPromptTest method testSignInButton_signsInAndSavesCredentials.

@Test
@Config(shadows = { BaseHelperShadow.class, ActivityHelperShadow.class })
public void testSignInButton_signsInAndSavesCredentials() {
    // initialize mocks
    new ActivityHelperShadow();
    reset(ActivityHelperShadow.sSaveSmartLock);
    WelcomeBackPasswordPrompt welcomeBackActivity = createActivity();
    EditText passwordField = (EditText) welcomeBackActivity.findViewById(R.id.password);
    passwordField.setText(TestConstants.PASSWORD);
    FirebaseUser mockFirebaseUser = mock(FirebaseUser.class);
    when(ActivityHelperShadow.sFirebaseAuth.signInWithEmailAndPassword(TestConstants.EMAIL, TestConstants.PASSWORD)).thenReturn(new AutoCompleteTask<AuthResult>(new FakeAuthResult(mockFirebaseUser), true, null));
    when(mockFirebaseUser.getDisplayName()).thenReturn(TestConstants.NAME);
    when(mockFirebaseUser.getEmail()).thenReturn(TestConstants.EMAIL);
    when(mockFirebaseUser.getPhotoUrl()).thenReturn(TestConstants.PHOTO_URI);
    Button signIn = (Button) welcomeBackActivity.findViewById(R.id.button_done);
    signIn.performClick();
    verify(ActivityHelperShadow.sFirebaseAuth).signInWithEmailAndPassword(TestConstants.EMAIL, TestConstants.PASSWORD);
    verifySmartLockSave(EmailAuthProvider.PROVIDER_ID, TestConstants.EMAIL, TestConstants.PASSWORD);
}
Also used : ActivityHelperShadow(com.firebase.ui.auth.testhelpers.ActivityHelperShadow) EditText(android.widget.EditText) Button(android.widget.Button) FakeAuthResult(com.firebase.ui.auth.testhelpers.FakeAuthResult) AuthResult(com.google.firebase.auth.AuthResult) FirebaseUser(com.google.firebase.auth.FirebaseUser) FakeAuthResult(com.firebase.ui.auth.testhelpers.FakeAuthResult) WelcomeBackPasswordPrompt(com.firebase.ui.auth.ui.accountlink.WelcomeBackPasswordPrompt) Test(org.junit.Test) Config(org.robolectric.annotation.Config) BuildConfig(com.firebase.ui.auth.BuildConfig)

Example 52 with FirebaseUser

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

the class AuthUI method getDeleteTask.

private Task<Void> getDeleteTask(CredentialTaskApi credentialHelper) {
    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    if (firebaseUser == null) {
        // If the current user is null, return a failed task immediately
        return Tasks.forException(new Exception("No currently signed in user."));
    }
    // Delete the Firebase user
    Task<Void> deleteUserTask = firebaseUser.delete();
    // Get all SmartLock credentials associated with the user
    List<Credential> credentials = SmartLockBase.credentialsFromFirebaseUser(firebaseUser);
    // For each Credential in the list, create a task to delete it.
    List<Task<?>> credentialTasks = new ArrayList<>();
    for (Credential credential : credentials) {
        credentialTasks.add(credentialHelper.delete(credential));
    }
    // Create a combined task that will succeed when all credential delete operations
    // have completed (even if they fail).
    final Task<Void> combinedCredentialTask = Tasks.whenAll(credentialTasks);
    // and return.
    return deleteUserTask.continueWithTask(new Continuation<Void, Task<Void>>() {

        @Override
        public Task<Void> then(@NonNull Task<Void> task) throws Exception {
            // Call getResult() to propagate failure by throwing an exception
            // if there was one.
            task.getResult(Exception.class);
            // Return the combined credential task
            return combinedCredentialTask;
        }
    });
}
Also used : Credential(com.google.android.gms.auth.api.credentials.Credential) Task(com.google.android.gms.tasks.Task) ArrayList(java.util.ArrayList) FirebaseUser(com.google.firebase.auth.FirebaseUser)

Example 53 with FirebaseUser

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

the class SignedInActivity method populateProfile.

@MainThread
private void populateProfile() {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user.getPhotoUrl() != null) {
        Glide.with(this).load(user.getPhotoUrl()).fitCenter().into(mUserProfilePicture);
    }
    mUserEmail.setText(TextUtils.isEmpty(user.getEmail()) ? "No email" : user.getEmail());
    mUserDisplayName.setText(TextUtils.isEmpty(user.getDisplayName()) ? "No display name" : user.getDisplayName());
    StringBuilder providerList = new StringBuilder(100);
    providerList.append("Providers used: ");
    if (user.getProviders() == null || user.getProviders().isEmpty()) {
        providerList.append("none");
    } else {
        Iterator<String> providerIter = user.getProviders().iterator();
        while (providerIter.hasNext()) {
            String provider = providerIter.next();
            if (GoogleAuthProvider.PROVIDER_ID.equals(provider)) {
                providerList.append("Google");
            } else if (FacebookAuthProvider.PROVIDER_ID.equals(provider)) {
                providerList.append("Facebook");
            } else if (EmailAuthProvider.PROVIDER_ID.equals(provider)) {
                providerList.append("Password");
            } else {
                providerList.append(provider);
            }
            if (providerIter.hasNext()) {
                providerList.append(", ");
            }
        }
    }
    mEnabledProviders.setText(providerList);
}
Also used : FirebaseUser(com.google.firebase.auth.FirebaseUser) MainThread(android.support.annotation.MainThread)

Example 54 with FirebaseUser

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

the class ChatActivity method attachRecyclerViewAdapter.

private void attachRecyclerViewAdapter() {
    Query lastFifty = mChatRef.limitToLast(50);
    mAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(Chat.class, R.layout.message, ChatHolder.class, lastFifty) {

        @Override
        public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
            holder.setName(chat.getName());
            holder.setText(chat.getMessage());
            FirebaseUser currentUser = mAuth.getCurrentUser();
            if (currentUser != null && chat.getUid().equals(currentUser.getUid())) {
                holder.setIsSender(true);
            } else {
                holder.setIsSender(false);
            }
        }

        @Override
        protected void onDataChanged() {
            // If there are no chat messages, show a view that invites the user to add a message.
            mEmptyListMessage.setVisibility(mAdapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
        }
    };
    // Scroll to bottom on new messages
    mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            mManager.smoothScrollToPosition(mMessages, null, mAdapter.getItemCount());
        }
    });
    mMessages.setAdapter(mAdapter);
}
Also used : Query(com.google.firebase.database.Query) RecyclerView(android.support.v7.widget.RecyclerView) FirebaseUser(com.google.firebase.auth.FirebaseUser)

Example 55 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project quickstart-android by firebase.

the class FacebookLoginActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_facebook);
    // Views
    mStatusTextView = (TextView) findViewById(R.id.status);
    mDetailTextView = (TextView) findViewById(R.id.detail);
    findViewById(R.id.button_facebook_signout).setOnClickListener(this);
    // [START initialize_auth]
    // Initialize Firebase Auth
    mAuth = FirebaseAuth.getInstance();
    // [END initialize_auth]
    // [START auth_state_listener]
    mAuthListener = new FirebaseAuth.AuthStateListener() {

        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // [START_EXCLUDE]
            updateUI(user);
        // [END_EXCLUDE]
        }
    };
    // [END auth_state_listener]
    // [START initialize_fblogin]
    // Initialize Facebook Login button
    mCallbackManager = CallbackManager.Factory.create();
    LoginButton loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
    loginButton.setReadPermissions("email", "public_profile");
    loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.d(TAG, "facebook:onSuccess:" + loginResult);
            handleFacebookAccessToken(loginResult.getAccessToken());
        }

        @Override
        public void onCancel() {
            Log.d(TAG, "facebook:onCancel");
            // [START_EXCLUDE]
            updateUI(null);
        // [END_EXCLUDE]
        }

        @Override
        public void onError(FacebookException error) {
            Log.d(TAG, "facebook:onError", error);
            // [START_EXCLUDE]
            updateUI(null);
        // [END_EXCLUDE]
        }
    });
// [END initialize_fblogin]
}
Also used : FacebookException(com.facebook.FacebookException) LoginResult(com.facebook.login.LoginResult) FirebaseUser(com.google.firebase.auth.FirebaseUser) LoginButton(com.facebook.login.widget.LoginButton) FirebaseAuth(com.google.firebase.auth.FirebaseAuth)

Aggregations

FirebaseUser (com.google.firebase.auth.FirebaseUser)100 AuthResult (com.google.firebase.auth.AuthResult)26 Intent (android.content.Intent)22 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)16 DataSnapshot (com.google.firebase.database.DataSnapshot)12 DatabaseError (com.google.firebase.database.DatabaseError)12 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)11 AuthCredential (com.google.firebase.auth.AuthCredential)11 DatabaseReference (com.google.firebase.database.DatabaseReference)11 ValueEventListener (com.google.firebase.database.ValueEventListener)11 View (android.view.View)10 Task (com.google.android.gms.tasks.Task)10 Button (android.widget.Button)9 NonNull (android.support.annotation.NonNull)8 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)7 FirebaseAuthUserCollisionException (com.google.firebase.auth.FirebaseAuthUserCollisionException)7 TextView (android.widget.TextView)6 Test (org.junit.Test)6 Context (android.content.Context)5 SharedPreferences (android.content.SharedPreferences)5