32

The Firebase createUser() method takes an email and password field, but what if I want to also allow the user a custom username similar to Snapchat, Instagram, StackOverflow etc? Is there any way to modify the existing method to accept that field as well or do I need to do push and manage this info manually and if so how?

This is my first attempt at storing the desired user info:

Firebase ref = new Firebase(firebaseURL);
ref.createUser(email, password, new Firebase.ValueResultHandler<Map<String, Object>>() {
@Override
public void onSuccess(Map<String, Object> result) {
System.out.println("Successfully created user account with uid: " + result.get("uid"));

//Sign user in
Firebase ref = new Firebase(firebaseURL);
ref.authWithPassword(email, password, new Firebase.AuthResultHandler() {

@Override
public void onAuthenticated(AuthData authData) {
System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());

//Save user info
Firebase userRef = new Firebase(firebaseURL + "Users/");
User user = new User(username, authData.getUid());
userRef.setValue(user);

Is this good practice? I figured storing the UID with the username may help me in the future handling changes etc. Also, should I be implementing the updateChildren() or push() method so the entries do not get overwritten if this is a social media app?

This is my second attempt:

  @Override
  public void onAuthenticated(AuthData authData) {
  System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());

  //Save user info and username
  Firebase ref = new Firebase(firebaseURL);
  Map<String, String> map = new HashMap<String, String>();
  map.put("email", email);
  map.put("username", username);
  map.put("provider", authData.getProvider());

  ref.child("users").child(authData.getUid()).setValue(map);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
young_souvlaki
  • 1,886
  • 4
  • 24
  • 28

3 Answers3

19

Show a form with three fields:

  1. Username
  2. Email address
  3. Password

Send the latter two to Firebase's createUser() method. Then in the completion callback for that, store all information in your Firebase database.

var userName, emailAddress, password;

// TODO: read these values from a form where the user entered them

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.createUser({
  email    : emailAddress,
  password : password
}, function(error, authData) {
  if (error) {
    console.log("Error creating user:", error);
  } else {
    // save the user's profile into the database so we can list users,
    // use them in Security and Firebase Rules, and show profiles
    ref.child("users").child(authData.uid).set({
      provider: authData.provider,
      name: userName
    });
  }
});

See this page on storing user data in the Firebase docs for more information.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 3
    Sorry for the late reply. This seems to be for Javascript. I need to for Java for Android. It seems to be setup slightly differently for each and I am having trouble translating. – young_souvlaki Sep 01 '15 at 20:04
  • 1
    Sure. **Edit** your question to include the code of your best effort and I'll have a look. – Frank van Puffelen Sep 01 '15 at 20:33
  • 1
    Will do! I really appreciate your willingness to help! Quick question first: the ```users``` parent and the ```authData.uid``` object...did you have to manually write those into the database or are those automatically created when using createUser? They don't show up in my firebase dashboard and I've already authenticated some pseudo-users. Are these objects hidden or inaccessible for user privacy purposes? – young_souvlaki Sep 01 '15 at 23:02
  • Okay I updated the question with my code. Thanks again for your help! – young_souvlaki Sep 02 '15 at 16:00
  • @FrankvanPuffelen Frank, could you give an example on how would you retrieve the username that was stored (after the user is logged in via email) – Onichan Nov 04 '15 at 07:06
  • 2
    the code is in JavaScript, It was supposed to be in Java! –  Mar 26 '16 at 00:52
  • What about showing just 2 fields - username and password and then matching the username with email stored in the database and sending that email along with the password to authenticate the user? This way user will not have to type email and yet he will be authenticated by email. – gegobyte Jul 28 '16 at 13:31
  • @FrankvanPuffelen - I've edited-Fixed your answer because the OP was looking for `Android` codes and no `JavaScript` codes. Please consider checking my edit for the future searching. – ʍѳђઽ૯ท Dec 18 '16 at 19:52
  • @Mohsen: thanks for the fix, but I rolled it back. While it may be closer to what OP wanted, your edit changed my answer a bit too much. It's better to post your code in a separate answer, so that devs can upvote that one if they find it useful. – Frank van Puffelen Dec 19 '16 at 01:13
  • @FrankvanPuffelen - I don't care about upvotes or something like that, Sorry! :) . In fact, I've had the same issue and saw this question on google and as you can see, The question tag is about `Android` that's what i'm talking about. And now i think under these circumstances, since you added the `JavaScript` codes again, I have to downvote your answer! Because **i myself AND of course the OP** was looking for Android codes and no JavaScript! – ʍѳђઽ૯ท Dec 19 '16 at 09:00
  • 3
    Wouldn't this cause a problem if the user's connection is broken after the account is created but before the username is set? – user2997154 Jun 14 '17 at 17:24
  • I agree with @user2997154. It's a shame we can't associate metadata with a user and need to either ignore the potential for a partially created user or add even more complexity by doing a rollback (that could also fail) – Dominic Aug 13 '19 at 20:54
  • I think this is a great answer and it deserves an edit. Also the link is broken. – ArchNoob Feb 13 '20 at 22:03
19

Yes, there is a way how to update user info in Firebase. All you need - to read this article in Firebase docs and implement method described here Update user info

Using this method you can update diplayName property of FirabaseUser object. Just set user name for displayName property and commit your changes.

I hope this will help you.

ostap_holub
  • 565
  • 7
  • 15
0

Here is one way to store the registered details in database,

Map<String, String> parameters = new HashMap<>();
FirebaseDatabase mFirebaseInstance;
parameters.put(Constant.TAG_USER, strUsrS.trim());
parameters.put(Constant.TAG_EMAIL, strEmailS.trim());
parameters.put(Constant.TAG_PASS, strPassS.trim());
//use this if needed(pushId)
String pushId = mFirebaseInstance.getReference(YOUR TABLE NAME).getRef().push().getKey();
parameters.put(Constant.TAG_KEY, pushId.trim());

mFirebaseInstance.getReference(YOUR TABLE NAME).getRef().child(strUsrS.trim()).setValue(parameters);

Try this implementation and execute... Thankyou

Hope that you aware about how to alter(create) table with required fields in firebase.

Related question & discussion with solutions

Arnold Brown
  • 1,330
  • 13
  • 28