Home > Net >  Firebase google authentication getCurrentUser.getEmail() returns null, but its returning value for g
Firebase google authentication getCurrentUser.getEmail() returns null, but its returning value for g

Time:06-24

I have created a google sign in using firebase followed by the official documentation of firebase.

enter image description here

public class UserLogin extends AppCompatActivity implements View.OnClickListener {

    DatabaseHelper user_tbl;
    private ImageView gmail;
    String TAG = "UserLoginActivity";
    private static final int RC_SIGN_IN = 9001;

    // [START declare_auth]
    private FirebaseAuth mAuth;
    // [END declare_auth]
    private GoogleSignInClient mGoogleSignInClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);
        user_tbl= new DatabaseHelper(this);

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken("284368548772-2gr7d5vqibjcd4jf08jsuhaquk21rnlu.apps.googleusercontent.com")
                .requestEmail()
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
        // [END config_signin]

        // [START initialize_auth]
        // Initialize Firebase Auth
        mAuth = FirebaseAuth.getInstance();
        setId();
        setEvents();
    }
   
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "----------------------------onActivityResult---------------------------");
        super.onActivityResult(requestCode, resultCode, data);
        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);
                Log.d(TAG, "firebaseAuthWithGoogle:"   account.getId());
                firebaseAuthWithGoogle(account.getIdToken());
            } catch (ApiException e) {
                // Google Sign In failed, update UI appropriately
                Log.w(TAG, "Google sign in failed", e);
            }
        }
    }

    private void firebaseAuthWithGoogle(String idToken) {
        Log.d(TAG, "------------------------firebaseAuthWithGoogle-----------------------");
        AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @RequiresApi(api = Build.VERSION_CODES.O)
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            String gmail_user_email = user.getEmail();
                            Log.d(TAG, "gmail_user_email" gmail_user_email);
                            String gmail_user_name = user.getDisplayName();
                            Log.d(TAG, "gmail_user_name" gmail_user_name);
                            String gmail_user_phone = user.getPhoneNumber();
                            Log.d(TAG, "gmail_user_phone" gmail_user_phone);
                            String gmail_user_id = user.getUid();
                            Log.d(TAG, "gmail_user_id" gmail_user_id);
                            new Display_Snackbar(UserLogin.this).Show_snack_bar("Sign in as " gmail_user_email);
                            int checkUser = user_tbl.CheckUser(gmail_user_email);
                            if(checkUser>0) {
                                Log.d(TAG, "user id exist in sqlite");
                                Boolean insert_login_details = user_tbl.InsertLoginDetails(gmail_user_email,"1");
                                syncData(gmail_user_email);
                                getDevices(gmail_user_email);
                            }else{
                                Log.d(TAG, "user id not exist in sqlite");
                                InsertNewUser(gmail_user_email,"",gmail_user_name,"");
                            }
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            new Display_Snackbar(UserLogin.this).Show_snack_bar("Gmail Sign in failed");
                        }
                    }
                });
    }
  
    private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

CodePudding user response:

In case of social login the main you will get id(social Token)

There are 2 points

  1. if user have permission to read data then you will get data like in case of facebook we can private profile data

  2. In your case you are not sending data in credentials you are passing token only in that case firebase don't save email this will return user with subId only.

    GoogleAuthProvider.getCredential(idToken, null)
    

Here you can get email and pass to credentials

GoogleSignInAccount account = task.getResult(ApiException.class);
Log.d(TAG, "firebaseAuthWithGoogle:"   account.getId());
  • Related