Search in sources :

Example 96 with FirebaseUser

use of com.google.firebase.auth.FirebaseUser in project Firebase-Login by jocelin09.

the class ProfileActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    // initializing firebase authentication object
    firebaseAuth = FirebaseAuth.getInstance();
    // Then we need a GoogleSignInOptions object
    // And we need to build it as below
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestIdToken(getString(R.string.default_web_client_id)).requestEmail().build();
    // Then we will get the GoogleSignInClient object from GoogleSignIn class
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
    // that means current user will return null
    if (firebaseAuth.getCurrentUser() == null) {
        // closing this activity
        finish();
        // starting login activity
        startActivity(new Intent(this, LoginActivity.class));
    }
    // getting current user
    FirebaseUser user = firebaseAuth.getCurrentUser();
    // initializing views
    textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
    buttonLogout = (Button) findViewById(R.id.buttonLogout);
    // displaying logged in user name
    // textViewUserEmail.setText("Welcome " + user.getEmail());
    textViewUserEmail.setText("Welcome ");
    // adding listener to button
    buttonLogout.setOnClickListener(this);
}
Also used : GoogleSignInOptions(com.google.android.gms.auth.api.signin.GoogleSignInOptions) Intent(android.content.Intent) FirebaseUser(com.google.firebase.auth.FirebaseUser)

Example 97 with FirebaseUser

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

the class AuthUI method delete.

/**
 * Delete the use from FirebaseAuth and delete any associated credentials from the Credentials
 * API. Returns a {@link Task} that succeeds if the Firebase Auth user deletion succeeds and
 * fails if the Firebase Auth deletion fails. Credentials deletion failures are handled
 * silently.
 *
 * @param context the calling {@link Context}.
 */
@NonNull
public Task<Void> delete(@NonNull final Context context) {
    final FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser == null) {
        return Tasks.forException(new FirebaseAuthInvalidUserException(String.valueOf(CommonStatusCodes.SIGN_IN_REQUIRED), "No currently signed in user."));
    }
    final List<Credential> credentials = getCredentialsFromFirebaseUser(currentUser);
    // Ensure the order in which tasks are executed properly destructures the user.
    return signOutIdps(context).continueWithTask(task -> {
        // Propagate exception if there was one
        task.getResult();
        if (!GoogleApiUtils.isPlayServicesAvailable(context)) {
            Log.w(TAG, "Google Play services not available during delete");
            return Tasks.forResult((Void) null);
        }
        final CredentialsClient client = GoogleApiUtils.getCredentialsClient(context);
        List<Task<?>> credentialTasks = new ArrayList<>();
        for (Credential credential : credentials) {
            credentialTasks.add(client.delete(credential));
        }
        return Tasks.whenAll(credentialTasks).continueWith(task1 -> {
            Exception e = task1.getException();
            Throwable t = e == null ? null : e.getCause();
            if (!(t instanceof ApiException) || ((ApiException) t).getStatusCode() != CommonStatusCodes.CANCELED) {
                // doesn't mean fully deleting the user failed.
                return task1.getResult();
            }
            return null;
        });
    }).continueWithTask(task -> {
        // Propagate exception if there was one
        task.getResult();
        return currentUser.delete();
    });
}
Also used : Scope(com.google.android.gms.common.api.Scope) Arrays(java.util.Arrays) Bundle(android.os.Bundle) UserInfo(com.google.firebase.auth.UserInfo) NonNull(androidx.annotation.NonNull) GoogleApiUtils(com.firebase.ui.auth.util.GoogleApiUtils) DrawableRes(androidx.annotation.DrawableRes) PhoneAuthProvider(com.google.firebase.auth.PhoneAuthProvider) GoogleAuthProvider(com.google.firebase.auth.GoogleAuthProvider) GoogleSignIn(com.google.android.gms.auth.api.signin.GoogleSignIn) RestrictTo(androidx.annotation.RestrictTo) Locale(java.util.Locale) Map(java.util.Map) GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) ProviderAvailability(com.firebase.ui.auth.util.data.ProviderAvailability) Log(android.util.Log) ActionCodeSettings(com.google.firebase.auth.ActionCodeSettings) IdentityHashMap(java.util.IdentityHashMap) FirebaseAuthProvider(com.google.firebase.auth.FirebaseAuthProvider) FirebaseUser(com.google.firebase.auth.FirebaseUser) Set(java.util.Set) FacebookAuthProvider(com.google.firebase.auth.FacebookAuthProvider) CredentialRequest(com.google.android.gms.auth.api.credentials.CredentialRequest) StyleRes(androidx.annotation.StyleRes) LoginManager(com.facebook.login.LoginManager) FlowParameters(com.firebase.ui.auth.data.model.FlowParameters) Preconditions(com.firebase.ui.auth.util.Preconditions) List(java.util.List) Tasks(com.google.android.gms.tasks.Tasks) Nullable(androidx.annotation.Nullable) AuthResult(com.google.firebase.auth.AuthResult) PhoneNumberUtils(com.firebase.ui.auth.util.data.PhoneNumberUtils) ApiException(com.google.android.gms.common.api.ApiException) Context(android.content.Context) ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) GithubAuthProvider(com.google.firebase.auth.GithubAuthProvider) Intent(android.content.Intent) ExtraConstants(com.firebase.ui.auth.util.ExtraConstants) TwitterAuthProvider(com.google.firebase.auth.TwitterAuthProvider) HashMap(java.util.HashMap) Parcel(android.os.Parcel) Task(com.google.android.gms.tasks.Task) Retention(java.lang.annotation.Retention) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AuthMethodPickerActivity(com.firebase.ui.auth.ui.idp.AuthMethodPickerActivity) CredentialUtils(com.firebase.ui.auth.util.CredentialUtils) CredentialsClient(com.google.android.gms.auth.api.credentials.CredentialsClient) StringDef(androidx.annotation.StringDef) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) Parcelable(android.os.Parcelable) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) TextUtils(android.text.TextUtils) CallSuper(androidx.annotation.CallSuper) CommonStatusCodes(com.google.android.gms.common.api.CommonStatusCodes) AuthCredential(com.google.firebase.auth.AuthCredential) Credential(com.google.android.gms.auth.api.credentials.Credential) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Collections(java.util.Collections) GoogleSignInOptions(com.google.android.gms.auth.api.signin.GoogleSignInOptions) FirebaseApp(com.google.firebase.FirebaseApp) RetentionPolicy(java.lang.annotation.RetentionPolicy) CredentialsClient(com.google.android.gms.auth.api.credentials.CredentialsClient) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) AuthCredential(com.google.firebase.auth.AuthCredential) Credential(com.google.android.gms.auth.api.credentials.Credential) List(java.util.List) ArrayList(java.util.ArrayList) FirebaseUser(com.google.firebase.auth.FirebaseUser) ApiException(com.google.android.gms.common.api.ApiException) FirebaseAuthInvalidUserException(com.google.firebase.auth.FirebaseAuthInvalidUserException) ApiException(com.google.android.gms.common.api.ApiException) NonNull(androidx.annotation.NonNull)

Example 98 with FirebaseUser

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

the class AnonymousUpgradeActivity method updateUI.

private void updateUI() {
    FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
    if (currentUser == null) {
        // Not signed in
        mBinding.anonSignIn.setEnabled(true);
        mBinding.beginFlow.setEnabled(false);
        mBinding.resolveMerge.setEnabled(false);
        mBinding.signOut.setEnabled(false);
    } else if (mPendingCredential == null && currentUser.isAnonymous()) {
        // Anonymous user, waiting for linking
        mBinding.anonSignIn.setEnabled(false);
        mBinding.beginFlow.setEnabled(true);
        mBinding.resolveMerge.setEnabled(false);
        mBinding.signOut.setEnabled(true);
    } else if (mPendingCredential == null && !currentUser.isAnonymous()) {
        // Fully signed in
        mBinding.anonSignIn.setEnabled(false);
        mBinding.beginFlow.setEnabled(false);
        mBinding.resolveMerge.setEnabled(false);
        mBinding.signOut.setEnabled(true);
    } else if (mPendingCredential != null) {
        // Signed in anonymous, awaiting merge conflict
        mBinding.anonSignIn.setEnabled(false);
        mBinding.beginFlow.setEnabled(false);
        mBinding.resolveMerge.setEnabled(true);
        mBinding.signOut.setEnabled(true);
    }
}
Also used : FirebaseUser(com.google.firebase.auth.FirebaseUser)

Example 99 with FirebaseUser

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

the class SignedInActivity method onCreate.

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
    if (currentUser == null) {
        startActivity(AuthUiActivity.createIntent(this));
        finish();
        return;
    }
    IdpResponse response = getIntent().getParcelableExtra(ExtraConstants.IDP_RESPONSE);
    mBinding = SignedInLayoutBinding.inflate(getLayoutInflater());
    setContentView(mBinding.getRoot());
    populateProfile(response);
    populateIdpToken(response);
    mBinding.deleteAccount.setOnClickListener(view -> deleteAccountClicked());
    mBinding.signOut.setOnClickListener(view -> signOut());
}
Also used : FirebaseUser(com.google.firebase.auth.FirebaseUser) IdpResponse(com.firebase.ui.auth.IdpResponse)

Example 100 with FirebaseUser

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

the class EmailLinkSignInHandler method handleNormalFlow.

private void handleNormalFlow(final AuthOperationManager authOperationManager, final EmailLinkPersistenceManager persistenceManager, final String email, final String link) {
    final AuthCredential emailLinkCredential = EmailAuthProvider.getCredentialWithLink(email, link);
    // Bug in core SDK - credential is mutated and won't be usable for sign in, so create
    // a new one to pass back. b/117425827
    final AuthCredential emailLinkCredentialForLinking = EmailAuthProvider.getCredentialWithLink(email, link);
    // Either regular sign in or anonymous user upgrade
    authOperationManager.signInAndLinkWithCredential(getAuth(), getArguments(), emailLinkCredential).addOnSuccessListener(authResult -> {
        persistenceManager.clearAllData(getApplication());
        FirebaseUser user = authResult.getUser();
        IdpResponse response = new IdpResponse.Builder(new User.Builder(EMAIL_LINK_PROVIDER, user.getEmail()).setName(user.getDisplayName()).setPhotoUri(user.getPhotoUrl()).build()).build();
        handleSuccess(response, authResult);
    }).addOnFailureListener(e -> {
        persistenceManager.clearAllData(getApplication());
        if (e instanceof FirebaseAuthUserCollisionException) {
            handleMergeFailure(emailLinkCredentialForLinking);
        } else {
            setResult(Resource.forFailure(e));
        }
    });
}
Also used : AuthCredential(com.google.firebase.auth.AuthCredential) ActionCodeResult(com.google.firebase.auth.ActionCodeResult) ProviderUtils(com.firebase.ui.auth.util.data.ProviderUtils) ProfileMerger(com.firebase.ui.auth.data.remote.ProfileMerger) EMAIL_LINK_PROVIDER(com.firebase.ui.auth.AuthUI.EMAIL_LINK_PROVIDER) NonNull(androidx.annotation.NonNull) User(com.firebase.ui.auth.data.model.User) EmailLinkPersistenceManager(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager) Resource(com.firebase.ui.auth.data.model.Resource) EmailLinkParser(com.firebase.ui.auth.util.data.EmailLinkParser) TaskFailureLogger(com.firebase.ui.auth.util.data.TaskFailureLogger) Task(com.google.android.gms.tasks.Task) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) Continuation(com.google.android.gms.tasks.Continuation) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) SignInViewModelBase(com.firebase.ui.auth.viewmodel.SignInViewModelBase) FirebaseUser(com.google.firebase.auth.FirebaseUser) SessionRecord(com.firebase.ui.auth.util.data.EmailLinkPersistenceManager.SessionRecord) TextUtils(android.text.TextUtils) AuthOperationManager(com.firebase.ui.auth.util.data.AuthOperationManager) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) AuthCredential(com.google.firebase.auth.AuthCredential) Nullable(androidx.annotation.Nullable) EmailAuthProvider(com.google.firebase.auth.EmailAuthProvider) Application(android.app.Application) FirebaseUiException(com.firebase.ui.auth.FirebaseUiException) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) AuthResult(com.google.firebase.auth.AuthResult) ErrorCodes(com.firebase.ui.auth.ErrorCodes) IdpResponse(com.firebase.ui.auth.IdpResponse) FirebaseAuthUserCollisionException(com.google.firebase.auth.FirebaseAuthUserCollisionException) FirebaseUser(com.google.firebase.auth.FirebaseUser) IdpResponse(com.firebase.ui.auth.IdpResponse)

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