user3489970 user3489970 - 1 month ago 16
Android Question

getting signed in user's details from firebase android

Welcome.java
activity

public class Welcome extends AppCompatActivity {

private SignInButton signInButton;
private static final int RC_SIGN_IN = 1;
private static final String TAG = "Welcome";

private GoogleApiClient mGoogleApiClient;
private FirebaseAuth mAuth ;
private FirebaseAuth.AuthStateListener mAuthListener;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
//onClickNew_user();
// Configure Google Sign In
mAuth = FirebaseAuth.getInstance();

mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

if(firebaseAuth.getCurrentUser() != null)
{

Intent intent = new Intent("com.cablocator.my.cablocator.SetupDriver");
startActivity(intent);
}

}
};
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();



GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

Toast.makeText(Welcome.this, "some error occurred", Toast.LENGTH_SHORT).show();
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();


signInButton = (SignInButton) findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setScopes(gso.getScopeArray());

signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

signIn();
}
});
}

@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}

private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed, update UI appropriately
// ...
}
}
}

private void firebaseAuthWithGoogle(GoogleSignInAccount account) {

AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(Welcome.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}


}


i want to get details of the signed in user on
SetupDriver
activity and store it in firebase database can anyone tell me how to do that. My current code in
SetupDriver
activity is this:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup_driver);

mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference();



onClickNextDriver();
onClickSignOut();

mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

if(firebaseAuth.getCurrentUser() == null) {
Intent intent = new Intent(SetupDriver.this,Welcome.class);
startActivity(intent);
}
}
};
}

public void onClickNextDriver()
{
test_et = (EditText) findViewById(R.id.test);
driver_btn = (Button)findViewById(R.id.setup_driver_btn);
driver_btn.setOnClickListener(

new View.OnClickListener()
{
@Override
public void onClick(View v) {

getDetails();
setDetails();
if(validate() && validateReg() && validateLicense())
{
//test_et.setText(license_no);
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

FirebaseUser user = firebaseAuth.getCurrentUser();

if(user != null) {
String name = user.getDisplayName();
String email = user.getEmail();
String uid = user.getUid();
mDatabase.child("name").setValue(name);

}

}
};

Toast.makeText(SetupDriver.this, "success", Toast.LENGTH_SHORT).show();

}
else {
Toast.makeText(SetupDriver.this, "please fill all the details", Toast.LENGTH_SHORT).show();
}
}
}
);
}

Answer

Your comment about "mGoogleClient is never used" pointed me to the solution. Change this:

GoogleApiClient  mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())...

to this

mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())...

You are creating a second instance of GoogleApiClient instead of defininf the class member used in signin().