问题
I created register activity with Firebase Authorization, and trying to register new user, but when I press register button I got exception from my code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etUsername = (EditText) findViewById(R.id.etUserName);
etEmail = (EditText) findViewById(R.id.etEmailRegiter);
etPassword = (EditText) findViewById(R.id.etPasswordRegiter);
etPasswordRe = (EditText) findViewById(R.id.etPasswordRegiterRe);
register = (Button) findViewById(R.id.btnRegister);
passwordRule = (LinearLayout) findViewById(R.id.password_rule);
etPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
passwordRule.setVisibility(View.VISIBLE);
}
});
mAuth = FirebaseAuth.getInstance();
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createAccount(etUsername.getText().toString(), etPassword.getText().toString());
}
});
}
private void createAccount(String email, String password) {
Log.d(LOG_TAG, "createAccount:" + email);
// if (!validateForm()) {
// return;
// }
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(LOG_TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
Log.d(LOG_TAG, ":" + mDatabase.child(PATH_TO_DATA_USERS));
// sendEmailVerification();
// Intent intent = new Intent(getContext(), MainActivity.class);
// intent.putExtra("user", user.getUid());
// startActivity(intent);
}
else {
try {
throw task.getException();
} catch(FirebaseAuthWeakPasswordException e) {
etPassword.setError("weak password");
etPassword.requestFocus();
} catch(FirebaseAuthInvalidCredentialsException e) {
etEmail.setError("invalid credential exception");
etEmail.requestFocus();
} catch(FirebaseAuthUserCollisionException e) {
etEmail.setError("collision exception");
etEmail.requestFocus();
} catch(Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
Log.w(LOG_TAG, "createUserWithEmail:failure", task.getException());
Log.d(LOG_TAG, ":" + (mAuth == null));
Toast.makeText(RegisterActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="50dp"
>
<EditText
android:id="@+id/etUserName"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="@string/hint_user_name"/>
<EditText
android:id="@+id/etEmailRegiter"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="@string/hint_email"/>
<EditText
android:id="@+id/etPasswordRegiter"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:hint="@string/hint_password"
android:inputType="textPassword"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/etPasswordRegiterRe"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:hint="@string/hint_password_re"
android:inputType="textPassword"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/password_rule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/password_rule"
android:layout_marginLeft="17dp"
android:layout_marginRight="17dp"
android:layout_marginTop="5dp"
android:textSize="17sp"/>
</LinearLayout>
<Button
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="@string/button_register"/>
</LinearLayout>
(Here in my code task.isSuccessful() returns me false and for to solve this problem i commented my validation method. So now no validation for password and email, but before I tried to run code with validation, which returns me same error)
and in logs get error like this
E/Volley: [187] BasicNetwork.performRequest: Unexpected response code 400 for https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=AIzaSyAPBVdM4hZpPBvTZrvE9gBjld0otb202Lg
I/AuthChimeraService: Error description received from server: {
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "INVALID_EMAIL"
}
],
"code": 400,
"message": "INVALID_EMAIL"
}
}
By the way I tried to sign up by creation new user by myself through console. It works well I can sign with this user.
Here is my gradle dependencies
compile 'com.google.firebase:firebase-database:10.0.1'
compile 'com.google.firebase:firebase-core:10.0.1'
compile 'com.google.firebase:firebase-auth:10.0.1'
回答1:
Probably the email user put in is not valid. Emails should be in this format = "myname@domain.com"
. If it does not match the format, that error is given by the server. Try putting in an email in that format. I will recommend you to read more about authentication here: https://firebase.google.com/docs/auth/custom-email-handler and here is a bit more about the firebase error codes: https://firebase.google.com/docs/reference/js/firebase.auth.Auth
EDIT:
You are sending "Username" to the email variable not the "Email"
Here is the code you need to fix: createAccount(etUsername.getText().toString(), etPassword.getText().toString());
You are sending username in the place of email so thats why the server is calling your email invalid. Change it to etEmail
.
Remember, when you are registering, you are registering with the Email not the Username!
回答2:
I'm starting using Firebase too, I'm using this code to register new users in my app and works well.
public void doRegister(final String username, String password) {
HideAppApplication.mAuth.createUserWithEmailAndPassword(username, password)
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("LOGIN", "createUserWithEmail:onComplete:" + task.isSuccessful());
if (task.isSuccessful()){
User user = new User();
user.setUserId(username);
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("user");
mDatabase.child(task.getResult().getUser().getUid()).setValue(user);
}
onRegisterRequested.registerRequested(task);
}
});
}
You can take a look to my project in github if you want.
Hope this can solve your problem.
来源:https://stackoverflow.com/questions/44797431/registration-user-using-firebase-throug-email-and-password