I have created a google sign in using firebase followed by the official documentation of firebase.
It has worked properly before. Now it is returning a null value for getCurrentUser.getEmail(). But it is returning values for other account details like user id, display name etc as shown below.
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);
}
