Tava estudando o FireBase e criei um metodo de login com email e senha, porém eu não queria usar este, então aprendi a usar o Google Auth, então resolvi associar a conta do google auth no banco de dados.
Quando logo com a conta, ele abre outra activity, nela tem um botão no qual eu queria adicionar a hora, eu uso o mesmo metodo que usei pra "criar a conta" no banco de dados, quando eu logo de novo no app, todos os dados são resetados. (Edit) coloquei um contador pra ver, e ele também reseta, basta logar novamente no app.
Primeiro login:
Click no botão:
Segundo login:
O que eu teria que mudar na hora de logar?
public class MainActivity extends AppCompatActivity {
FirebaseAuth auth;
FirebaseUser fireuser;
DatabaseReference rootReference;
GoogleSignInClient mGoogleSignInClient;
SignInButton gbutton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
auth = FirebaseAuth.getInstance();
rootReference = FirebaseDatabase.getInstance().getReference();
gbutton = (SignInButton)findViewById(R.id.btnGoogle);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this,gso);
gbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, 101);
}
});
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
auth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
fireuser = auth.getCurrentUser();
User myUserInsertObj = new User(fireuser.getEmail());
rootReference.child("User").child(fireuser.getUid()).setValue(myUserInsertObj)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(getApplicationContext(),"User logged in successfully!", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(),Logado.class);
startActivity(i);
finish();
} else {
Toast.makeText(getApplicationContext(),"Error logging in with Google Auth.", Toast.LENGTH_SHORT).show();
}
}
});
} else {
// If sign in fails, display a message to the user.
Toast.makeText(getApplicationContext(), "Could not log in user", Toast.LENGTH_SHORT).show();
}
}
});
}
@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 == 101) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
}
}
}
}