Storing user data example - display name not showing in database

跟風遠走 提交于 2019-12-10 12:29:15

问题


A bit confused why there's no display name being displayed in my Firebase database, I've followed the steps in the example on their website and didn't get the expected result which they showed. Here's the example code.

final Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.authWithPassword("jenny@example.com", "correcthorsebatterystaple",
    new Firebase.AuthResultHandler() {
    @Override
    public void onAuthenticated(AuthData authData) {
        // Authentication just completed successfully :)
        Map<String, String> map = new HashMap<String, String>();
        map.put("provider", authData.getProvider());
        if(authData.getProviderData().containsKey("displayName")) {
            map.put("displayName", authData.getProviderData().get("displayName").toString());
        }
        ref.child("users").child(authData.getUid()).setValue(map);
    }
    @Override
    public void onAuthenticationError(FirebaseError error) {
        // Something went wrong :(
    }
});

Whenever I tried this example code the expected result was to be something similar to what is shown below.

{
  "users": {
    "6d914336-d254-4fdb-8520-68b740e047e4": {
      "displayName": "alanisawesome",
      "provider": "password"
    },
    "002a448c-30c0-4b87-a16b-f70dfebe3386": {
      "displayName": "gracehop",
      "provider": "password"
    }
  }
}

Instead of showing something similar to the above, in my database only the provider is shown and not the displayName. Is there any necessary steps required to get the displayName to show or should it be there automatically?

I tried to look into this to see what was included within authData.getProviderData() and this was what was produced in the console when I printed it out.

01-25 17:58:51.921 15651-15651/<package-name> I/System.out: {email=j.joe@hotmail.co.uk, isTemporaryPassword=false, profileImageURL=https://secure.gravatar.com/avatar/de53da9874178adb1a44b392ba5bed2f?d=retro}

So from the result produced it's left me wondering if the key "displayName" was formerly automatically created from the email and stored by default in authData.getProviderData() and that's why it is no longer shown.

Would appreciate it if someone could explain where the displayName in the database comes from because judging by the expected result it seems I've missed something.

Thanks!


回答1:


An email+password account does not have an associated display name. It only requires an email address and a password.

You can definitely require the user to enter additional information. Just don't pass it to createUser() and don't overwrite it after calling authWithPassword().

Update: since the update to Firebase Authentication released at Google I/O 2016, there is now a display name property for each user. So you can also keep the display name in Firebase Authentication itself (although you won't be able to query/search for it).



来源:https://stackoverflow.com/questions/35004356/storing-user-data-example-display-name-not-showing-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!