Warm tip: This article is reproduced from serverfault.com, please click

Is it possible to get first and last name from Google Authentication on Firebase?

发布于 2020-11-28 01:06:55

On my program, I use Google Authentication on Firebase. But when I try to retrieve the first name only, I only saw that it is possible to get the display name using .getDisplayName(), which consisted of the entire first and last name. I was wondering if there are any ways to get the first name only.

        int index = mAuth.getCurrentUser().getDisplayName().indexOf(" ");
        String first = mAuth.getCurrentUser().getDisplayName().substring(0, index);
        String last = mAuth.getCurrentUser().getDisplayName().substring(index + 1);
        Map<String, Object> user = new HashMap<>();

        user.put("first", first);
        user.put("last", last);
        user.put("uid", mAuth.getCurrentUser().getUid());
        user.put("email", mAuth.getCurrentUser().getEmail());
        user.put("phone", mAuth.getCurrentUser().getPhoneNumber());

I tried using Index of and getting substring of the first and last name, but that doesn't seem to work since I believe it is possible to have multiple spaces in first and last names.

If anyone could help me out here that'd be great,

Thank you

Questioner
ryan chen
Viewed
0
Ilyasse Salama 2020-11-28 09:40:31

As Doug said, display name is a free form, you can't know where is the user's first or last name. However, you can use the following method, but I don't guarantee that it will work for all names.

String fullname = "Ilyasse Salama";
String[] parts = fullname.split("\\s+");
String firstname = parts[0];
String lastname = parts[1];