我已将Firebase添加到我的Android项目中。我已创建createUserWithEmailAndPassword()
来验证电子邮件和密码。现在,我还要求在创建新帐户时将用户名存储在Firebase数据库中。
我在线阅读,onAuthenticated(AuthData authData)
用于存储用户名。但是,请指导我在哪里实际放置onAuthenticated(AuthData authData)
方法,以便在电子邮件和电子邮件中调用它。密码验证完成。
目前,我已将这两种方法都放在private void UserSignUp()
中,当点击注册按钮时调用这些方法。谢谢。下面给出的是代码:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
emailid=(EditText)findViewById(R.id.emailText);
username=(EditText)findViewById(R.id.usernameText);
pwd=(EditText)findViewById(R.id.passwordText);
signup=(Button)findViewById(R.id.signupbutton);
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
UserSignUp();
}
});
}
private void UserSignUp()
{
firebaseAuth = FirebaseAuth.getInstance();
email = emailid.getText().toString().trim();
password = pwd.getText().toString().trim();
uname = username.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter Email address", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(uname)) {
Toast.makeText(getApplicationContext(), "Enter username", Toast.LENGTH_SHORT).show();
return;
}
if (uname.length() < 5) {
Toast.makeText(getApplicationContext(), "Invalid: Username must be greater than 5 characters", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 8) {
Toast.makeText(getApplicationContext(), "Invalid: Password must be greater than 8 characters", Toast.LENGTH_SHORT).show();
return;
}
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
{
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
if(task.isSuccessful())
{
Toast.makeText(SignUpActivity.this,"Successfully registered",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(SignUpActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
}
}
});
Firebase.AuthResultHandler authResultHandler=new Firebase.AuthResultHandler()
{
@Override
public void onAuthenticated(AuthData authData)
{
firebaseRef = new Firebase(FIREBASE_URL);
Map<String, String> map = new HashMap<String, String>();
map.put("email", email);
map.put("username", uname);
map.put("provider", authData.getProvider());
firebaseRef.child("users").child(authData.getUid()).setValue(map);
}
@Override
public void onAuthenticationError(FirebaseError firebaseError)
{
}
};
}
答案 0 :(得分:1)
以您的命名为例:
您可以在onCreate中或在调用createUserWithEmailAndPassword之前(例如在onCreate中)附加名为authResultHandler的AuthResultHandler。您还应该将auth listener注册为一个字段,以便以后能够引用它(取消注册)。
Firebase.AuthResultHandler authResultHandler=new Firebase.AuthResultHandler()
{
@Override
public void onAuthenticated(AuthData authData)
{
firebaseRef = new Firebase(FIREBASE_URL);
Map<String, String> map = new HashMap<String, String>();
map.put("email", email);
map.put("username", uname);
map.put("provider", authData.getProvider());
firebaseRef.child("users").child(authData.getUid()).setValue(map);
}
@Override
public void onAuthenticationError(FirebaseError firebaseError)
{
}
};
firebaseAuth.addAuthStateListener(authResultHandler);
您还需要在onDestroy中取消注册您的监听器:
@Override
public void onDestroy() {
super.onDestroy();
if (this.firebaseAuth != null) {
firebaseAuth.removeAuthStateListener(authResultHandler);
}
}
这应该足以让你走上正确的道路。