[英]Firebase Email/Password sign up not working
So I have a sign up function in my app, what it does is ask for an email and password and then creates an account with those parameters.所以我的应用程序中有一个注册功能,它的作用是要求提供电子邮件和密码,然后使用这些参数创建一个帐户。 Then the app logs the user with the newly created account and requests the email and uid that Firestore created, and puts it in a collection.
然后应用程序使用新创建的帐户记录用户并请求 Firestore 创建的电子邮件和 uid,并将其放入一个集合中。
For whatever reason, the app crashes when I try to retrieve the account details from Firebase and when I check it, no account was ever created and no collection was made (Tho it never got to that point so I guess yeah).无论出于何种原因,当我尝试从 Firebase 检索帐户详细信息时,应用程序崩溃,当我检查它时,没有创建帐户,也没有创建任何集合(但它从未达到那个点,所以我想是的)。
What am I doing wrong?我究竟做错了什么?
This is the code (And no, for this project I don't need any username):这是代码(不,对于这个项目,我不需要任何用户名):
public class Register extends AppCompatActivity {
EditText RegisterEditTextEmail, RegisterEditTextPassword, RegisterEditTextPasswordAgain;
Button RegisterButton;
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseFirestore db = FirebaseFirestore.getInstance();
boolean emailGood = false, passwordGood = false, passwordMatch = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
RegisterButton = findViewById(R.id.RegisterButton);
RegisterEditTextEmail = findViewById(R.id.RegisterEditTextEmail);
RegisterEditTextPassword = findViewById(R.id.RegisterEditTextPassword);
RegisterEditTextPasswordAgain = findViewById(R.id.RegisterEditTextPasswordAgain);
RegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = RegisterEditTextEmail.getText().toString();
String password = RegisterEditTextPassword.getText().toString();
String passwordAgain = RegisterEditTextPasswordAgain.getText().toString();
........
checking if the email and password are good
........
if (emailGood && passwordGood && passwordMatch) {
auth.createUserWithEmailAndPassword(RegisterEditTextEmail.getText().toString(), RegisterEditTextPassword.getText().toString())
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(Task<AuthResult> task) {
if(task.isComplete()){
FirebaseUser user = auth.getCurrentUser();
Map<String, Object> map = new HashMap<>();
map.put("email", user.getEmail()); <---- crashes here
map.put("uuid", user.getUid());
db.collection("users")
.document(user.getUid().toString())
.set(map)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Intent intent = new Intent(Register.this, MainActivity.class);
startActivity(intent);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Register.this, e.getMessage().toString(), Toast.LENGTH_LONG);
}
});
}
}
});
}
}
});
}
}
The error I get:我得到的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.clockin, PID: 3607
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getEmail()' on a null object reference
at com.example.clockin.Register$1$1.onComplete(Register.java:74)
at com.google.android.gms.tasks.zzj.run(com.google.android.gms:play-services-tasks@@17.2.0:4)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Edit: Someone suggested a NullPointer solution, I guess I failed to elaborate further.编辑:有人提出了 NullPointer 解决方案,我想我没有进一步阐述。 My problem isn't that the account is returned as null, my problem is that for whatever reason, FirebaseAuth refuses to create an account in the first place.
我的问题不是该帐户返回为 null,我的问题是无论出于何种原因,FirebaseAuth 首先拒绝创建帐户。 And as a result, the account is null.
结果,该帐户为空。 Why won't FirebaseAuth make an account?
为什么 FirebaseAuth 不创建帐户?
Edit 2: Apparently my API key in Google Cloud is expired, I tried looking around for a fix but didn't manage to find one, anyone knows how to fix it?编辑 2:显然我在 Google Cloud 中的 API 密钥已过期,我尝试四处寻找修复程序,但没有找到,有人知道如何修复吗?
The problem is in this snippet:问题出在这个片段中:
auth.createUserWithEmailAndPassword(RegisterEditTextEmail.getText().toString(), RegisterEditTextPassword.getText().toString())
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(Task<AuthResult> task) {
if(task.isComplete()){
FirebaseUser user = auth.getCurrentUser();
Map<String, Object> map = new HashMap<>();
map.put("email", user.getEmail());
When a Task
completes it can either be successful or unsuccessful and you need to handle both case separately.当
Task
完成时,它可以是成功的,也可以是不成功的,您需要分别处理这两种情况。 Only when the task is successful will auth.getCurrentUser()
have a value.只有当任务成功时
auth.getCurrentUser()
才会有值。
So you should only try to access that (and its getEmail
) when the task is successful:所以你应该只在任务成功时尝试访问它(和它的
getEmail
):
public void onComplete(Task<AuthResult> task) {
if(task.isSuccessful()){ // 👈 Change this test
FirebaseUser user = auth.getCurrentUser();
Map<String, Object> map = new HashMap<>();
map.put("email", user.getEmail());
When the task failed, you can learn more about why it failed by logging the exception it contains.当任务失败时,您可以通过记录它包含的异常来详细了解它失败的原因。 So:
所以:
public void onComplete(Task<AuthResult> task) {
if(task.isSuccessful()){ // 👈 Change this test
FirebaseUser user = auth.getCurrentUser();
Map<String, Object> map = new HashMap<>();
map.put("email", user.getEmail());
...
}
else {
Log.e("AUTH", "Creating user failed", task.getException()); // 👈
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.