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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.