PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 2, 2022

[FIXED] how to get the firebase UID of the user who logged in via google?

 November 02, 2022     android, firebase, firebase-authentication, java     No comments   

Issue

I'm trying to get UID of user who signed in via google.

When I'm using FirebaseAuth.getInstance().getCurrentUser().getUid() its obviously shows error.

When I'm using GoogleSignIn.getLastSignedInAccount(getContext()).getId its show incorrect ID .

How could I get this his UID?

There is code that I used to sign in via google. I get it from https://firebase.google.com/docs/auth/android/google-signin

signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken("R.string.default_client_id")
            .requestEmail()
            .build();

    signInClient = GoogleSignIn.getClient(this, signInOptions);

    signIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent sign = signInClient.getSignInIntent();
            startActivityForResult(sign, GOOGLE_SIGN_IN_CODE);
        }
    });

 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == GOOGLE_SIGN_IN_CODE){
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = task.getResult(ApiException.class);
            System.out.println("firebaseAuthWithGoogle:" + account.getId());
            firebaseAuthWithGoogle( account.getIdToken());
        } catch (ApiException e) {
            // Google Sign In failed, update UI appropriately
        }
    }
}
private void firebaseAuthWithGoogle(String idToken) {
    AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        FirebaseUser user = mAuth.getCurrentUser();
                        startActivity(new Intent(MainActivity.this,NavActivity.class));
                        updateUI(user);
                    } else {
                        updateUI(null);
                    }
                }
            });
}

This is how I get the UID

String uid = "";
    if(firebaseUser!=null ){
        uid = firebaseUser.getUid();
    }

Solution

You need to use your google sign-in token to log into Firebase.

FirebaseAuth.getInstance().signInWithCredential(GoogleAuthProvider.getCredential(googleSignInAccount.getIdToken(), null))

Because your FirebaseUser and your Google account are using two different authorizations, therefore can't be used interchangeably.



Answered By - Abdullah Z Khan
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing