Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you include a username when storing email and password using Firebase (BaaS) in an Android app?

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); 
like image 633
young_souvlaki Avatar asked Aug 22 '15 00:08

young_souvlaki


People also ask

How do I use Firebase email and password authentication?

If you haven't yet connected your app to your Firebase project, do so from the Firebase console. Enable Email/Password sign-in: In the Firebase console, open the Auth section. On the Sign in method tab, enable the Email/password sign-in method and click Save.

How do I use Firebase for user authentication?

How does it work? To sign a user into your app, you first get authentication credentials from the user. These credentials can be the user's email address and password, or an OAuth token from a federated identity provider. Then, you pass these credentials to the Firebase Authentication SDK.


2 Answers

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.

like image 171
Frank van Puffelen Avatar answered Sep 24 '22 01:09

Frank van Puffelen


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.

like image 39
ostap_holub Avatar answered Sep 20 '22 01:09

ostap_holub