通过电子邮件和密码使用 firebase 注册用户?
Registration user using firebase throug email and password?
我使用 Firebase 授权创建了注册 activity,并尝试注册新用户,但是当我按下注册按钮时,我的代码出现异常
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代码:
<?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>
(在我的代码中 task.isSuccessful() returns 我是假的,为了解决这个问题,我评论了我的验证方法。所以现在没有验证密码和电子邮件,但在我尝试之前运行 验证代码,returns 我同样的错误)
并且在日志中出现这样的错误
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"
}
}
顺便说一句,我尝试通过控制台自己创建新用户来注册。效果很好我可以和这个用户签约。
这是我的 gradle 依赖项
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'
可能是用户输入的电子邮件无效。电子邮件应采用这种格式 = "myname@domain.com"
。如果它与格式不匹配,则该错误由服务器给出。尝试以该格式输入电子邮件。我建议您在此处阅读有关身份验证的更多信息: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
编辑:
您正在将 "Username" 发送到电子邮件变量而不是 "Email"
这是您需要修复的代码:
createAccount(etUsername.getText().toString(), etPassword.getText().toString());
您发送的是用户名而不是电子邮件,这就是服务器称您的电子邮件无效的原因。将其更改为 etEmail
。
请记住,当您注册时,您注册的是电子邮件而不是用户名!
我也开始使用 Firebase,我正在使用此代码在我的应用程序中注册新用户并且运行良好。
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);
}
});
}
如果你愿意,可以在github中查看我的项目。
希望这可以解决您的问题。
我使用 Firebase 授权创建了注册 activity,并尝试注册新用户,但是当我按下注册按钮时,我的代码出现异常
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代码:
<?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>
(在我的代码中 task.isSuccessful() returns 我是假的,为了解决这个问题,我评论了我的验证方法。所以现在没有验证密码和电子邮件,但在我尝试之前运行 验证代码,returns 我同样的错误)
并且在日志中出现这样的错误
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"
}
}
顺便说一句,我尝试通过控制台自己创建新用户来注册。效果很好我可以和这个用户签约。
这是我的 gradle 依赖项
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'
可能是用户输入的电子邮件无效。电子邮件应采用这种格式 = "myname@domain.com"
。如果它与格式不匹配,则该错误由服务器给出。尝试以该格式输入电子邮件。我建议您在此处阅读有关身份验证的更多信息: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
编辑:
您正在将 "Username" 发送到电子邮件变量而不是 "Email"
这是您需要修复的代码:createAccount(etUsername.getText().toString(), etPassword.getText().toString());
您发送的是用户名而不是电子邮件,这就是服务器称您的电子邮件无效的原因。将其更改为 etEmail
。
请记住,当您注册时,您注册的是电子邮件而不是用户名!
我也开始使用 Firebase,我正在使用此代码在我的应用程序中注册新用户并且运行良好。
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);
}
});
}
如果你愿意,可以在github中查看我的项目。
希望这可以解决您的问题。