问题
I am trying to implement the registration of a username in Firebase since it only gives me methods that are like the createUserWithEmailAndPassword(), but you can not create a username and I do not know how to do it...
Below is my register code,but I also want to enter a user name that can use it, when the user publishes something etc, such as the getEmail() method, also being able to have a getUsername().
auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if(!task.isSuccessful()){
Toast.makeText(RegisterActivity.this, "Something went wrong :(" , Toast.LENGTH_LONG).show();
}else{
startActivity(new Intent(RegisterActivity.this, UsernameARegistrarActivity.class));
finish();
}
}
});
}
My model class:
public class User implements Serializable{
private String user;
private String email;
private String Uid;
private String username;
public User() {
}
public User(String user) {
this.user = user;
}
public User(String user, String email, String uid, String username) {
this.user = user;
this.email = email;
Uid = uid;
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUid() {
return Uid;
}
public void setUid(String uid) {
Uid = uid;
}
}
回答1:
If you're using email/password auth with a username instead, you can simply index the users to email addresses in a sep path. Then when a user authenticates, look up their email in the index:
/user_to_email_index/$user_name/ https://www.firebase.com/docs/web/guide/login/custom.html
回答2:
Try this,
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
User user = new User(firebaseUser.getUid(),
firebaseUser.getEmail(),
, firstName, photoURL); // add value as bean object
database.child(ARG_USERS)
.child(firebaseUser.getUid())
.setValue(user)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// user data updated in firebase
}
});
回答3:
You cannot store custom data within a firebase user per se, according to firebase documentation
You cannot add other properties to the Firebase User object directly; instead, you can store the additional properties in your Firebase Realtime Database.
Since each registered user has a unique id that you can access with
FirebaseAuth.getInstance().getCurrentUser().getUid()
once the user logs in, you can create a table in the realtime database that stores data of each user using your User class. Then you can query the database for the logged in user using the snippet above for the Uid.
More info here: https://firebase.google.com/docs/auth/users
来源:https://stackoverflow.com/questions/43445169/how-can-i-register-a-username-in-firebase