Steve Doson Steve Doson - 1 month ago 9
Javascript Question

How to get just the first name when a user logs in using social media with firebase?

Here is the code that I am using to get the details of the user when they log in using google, facebook or twitter.

firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
var displayName = user.displayName;
var email =;
var emailVerified = user.emailVerified;
var photoURL = user.photoURL;
var uid = user.uid;
var phoneNumber = user.phoneNumber;
var providerData = user.providerData;
user.getToken().then(function(accessToken) {

console.debug('user', user);

document.getElementById('name').textContent = displayName;
document.getElementById("avatar").src = photoURL;


} else {
console.log('not logged in');



Then in the html by writing
<p id="name"></p>
the entire name of the user is displayed.

How do I display just the first name of the user?

Answer Source

You can do it like this;

document.getElementById('name').textContent = (displayName.split(' '))[0];