Following information on similar questions on Stack I've ended up with the following:
Database:
{
"user" : {
"xbStJyCtfzf472" : {
"avatar" : "avatar",
"email" : "[email protected]",
"fbId" : 10154200898,
"lastLogin" : "Tue Mar 14 22:31:36 GMT+01:00 2017",
"registerDate" : "Tue Mar 14 22:31:36 GMT+01:00 2017",
"uid" : "xbStJyCwY5Ttfzf472",
"username" : "myName"
}
},
"usernames" : {
"myName" : "xbStJyCtfzf472"
}
}
Rules:
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"users": {
"$uid": {
".write": "auth !== null && auth.uid === $uid",
".read": "auth !== null && auth.provider === 'password'",
"username": {
".validate": "
!root.child('usernames').child(newData.val()).exists() ||
root.child('usernames').child(newData.val()).val() == $uid"
}
}
}
}
}
Android Java Code:
public void addUserToDatabase(final long fbId){
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
final String username = "myName";
//add the user
mDatabase.child("usernames").child(username).runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
if (mutableData.getValue() == null) {
mutableData.setValue(user.getUid());
return Transaction.success(mutableData);
}
return Transaction.abort();
}
@Override
public void onComplete(DatabaseError firebaseError, boolean commited, DataSnapshot dataSnapshot) {
if (commited) {
String email = user.getEmail();
String uid = user.getUid();
String avatar = "avatar1";
long mFbId = fbId;
String registered = Calendar.getInstance().getTime().toString();
String lastLogin = Calendar.getInstance().getTime().toString();
Log.w("AUTH", "username saved");
mDatabase.child("user").child(uid).setValue(new User(username, email, uid, avatar, mFbId, lastLogin, registered));
} else {
Log.w("AUTH", "username exists");
}
}
});
}
}
This works, since if I try to register accounts with the same userName, nothing is created in my database. However, A user is created in my Authentication/Users. I can see this leading to all kinds of issues.
How should I be forcing unique usernames in my database in a way that does not register the user until the check has been made?
EDIT: To clarify, I can detect that the username is taken, and prompt the user to input another. However, what if the user gets a call/exits my app while doing this, then their email will be authenticated (i.e they cant register again), but they won't have a User-object in my database, so they will never be able to use my app.
Short Answer:
First register user, then prompt them to put in their username, when they do, create their User-object in your database.
Solution to problem:
If a user for some reasons exits the app while deciding on a username, leading their email to be stored in the authentication-table without a User
-object in the database, we have a problem.
To solve this, whenever a user signs in, we can check if their UID exists in the User
-table, if it does, great! If not, then we prompt them to enter a username again, and then create the User-object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With