我有一个系统可以将 createUserWithEmailAndPassword 数据保存到实时数据库和身份验证数据库中。但是在使用谷歌登录创建了一个类似的系统之后,没有任何内容会保存到数据库中,也没有任何内容保存到身份验证数据库中。
我试过使用 Log.e,我试过调试应用程序,也试过解码代码...
这里是一些代码:
package com.brandshopping.brandshopping;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.HashMap;
public class LoginOrSignupActivity extends AppCompatActivity {
private Button LoginBtn, RegisterWithEmailBtn, RegisterWithGoogleBtn;
private String Tag;
private ProgressDialog LoadingBar;
private FirebaseDatabase firebasedatabase = FirebaseDatabase.getInstance();
private DatabaseReference database = firebasedatabase.getReference();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_or_signup);
LoadinGUI();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso); //Create Google sign in object
LoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginOrSignupActivity.this, LogInActivity.class));
}
});
RegisterWithEmailBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginOrSignupActivity.this, RegisterActivity.class));
}
});
RegisterWithGoogleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Register_WithGoogle_btn:
signIn();
break;
}
}
});
}
private void signIn() {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(LoginOrSignupActivity.this, gso); //Create Google sign in object
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
int RC_SIGN_IN = 100;
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int RC_SIGN_IN = 100;
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
Toast.makeText(this, "Register/signin successful", Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginOrSignupActivity.this, AccountInfoActivity.class));
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Toast.makeText(this, "Log in failed", Toast.LENGTH_SHORT).show();
Log.e(Tag, "error: ");
startActivity(new Intent(LoginOrSignupActivity.this, AccountInfoActivity.class));
}
}
void SaveToDataBase(){
database.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
GoogleSignInAccount Guser = GoogleSignIn.getLastSignedInAccount(LoginOrSignupActivity.this);
String EmailWithEtheraRemoved = Guser.getEmail().replace(".", " ");
if(!(dataSnapshot.child("Users").child(EmailWithEtheraRemoved).exists())){
LoadingBar.setMessage("Please wait while we load the credentialls in");
LoadingBar.setTitle("Register");
LoadingBar.setCanceledOnTouchOutside(false);
LoadingBar.show();
HashMap<String, Object> Userdatamap = new HashMap<>();
Userdatamap
.put("Email", Guser.getEmail());
Userdatamap
.put("Phone number", "Google intigrated sign in does not allow phone number requesting... This will be fixed in later patches");
Userdatamap
.put("Name", Guser.getGivenName() + Guser.getFamilyName());
if(Guser != null){
Userdatamap
.put("Created with", "Intigrated Google sign in");
}
database
.child("Users")
.child(EmailWithEtheraRemoved)
.updateChildren(Userdatamap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
LoadingBar.dismiss();
Toast.makeText(LoginOrSignupActivity.this, "Database save successful", Toast.LENGTH_SHORT).show();
Log.e("SignUpError :", task
.getException()
.getMessage());
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(LoginOrSignupActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
Log.e(Tag, "error: ");
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
void LoadinGUI(){
LoginBtn = (Button) findViewById(R. id. Login_btn);
RegisterWithEmailBtn = (Button) findViewById(R. id. Register_WithEmail_btn);
RegisterWithGoogleBtn = (Button) findViewById(R. id. Register_WithGoogle_btn);
}
}
我希望应用程序将信息保存到实时数据库和身份验证数据库中。这些似乎都不起作用......
最佳答案
登录成功后忘记调用SaveToDataBase
。这就是为什么没有日志和数据库条目的原因。
关于java - Firebase Auth 和 Firebase 数据库不保存 Google 注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56957729/