Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Functions for Firebase Auth Events for Social Provider

Trying to update the User data in the Realtime Database when a User is created. This is my code:

const functions = require('firebase-functions');
const promise = require('request-promise');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

var omitBy = require('lodash.omitby');
var isNil = require('lodash.isnil');

'use strict';

exports.userCreated = functions.auth.user().onCreate(event => {

  let request = admin.auth().getUser(event.data.uid)
  .then(function(user) {
    console.log("Successfully fetched user data: ", user.toJSON());

    var email, firstName, lastName, photoURL;

    for (var provider of user.providerData) {

      if (provider.email) {
        email = provider.email;
      }

      if (provider.photoURL) {
        photoURL = provider.photoURL;
      }

      if (provider.displayName) {
        const names = provider.displayName.split(' ');
        firstName = names[0];

        if (names.length > 1) {
          lastName = names[names.length - 1];
        }
      }
    }

    var values = omitBy({
      email: email,
      first_name: firstName,
      last_name: lastName,
      license_agreement_version: '1.1',
      image_url: photoURL
    }, isNil);

    admin.database().ref('users/' + user.uid).set(values);
  })
  .catch(function(error) {
    console.error("Error Fetching User: ", error);
  });

  return request;
});

However, when a User is created via. Facebook, the provider data isn't provided. This is the console log:

Successfully fetched user data: { uid: 'exampleUID',
  email: undefined,
  emailVerified: false,
  displayName: undefined,
  photoURL: undefined,
  disabled: false,
  metadata: 
   { lastSignedInAt: 2017-03-16T19:40:59.000Z,
     createdAt: 2017-03-16T19:40:59.000Z },
  providerData: [] }

Am I doing something wrong, or will this data not be provided on creation?

like image 612
Cody Winton Avatar asked Oct 29 '22 10:10

Cody Winton


1 Answers

You don't need to do another call to admin.auth().getUser(...). Conveniently, the event.data that you receive in this function is already a UserRecord!

Here's my very simple code to print Auth events:

var functions = require('firebase-functions');

exports.helloAuth = functions.auth.user().onCreate(event => {
  console.log("User created: " + JSON.stringify(event));
});

And here's what that outputs when I log in via Facebook:

{
  "displayName": "Robert-Jan Huijsman",
  "email": "[email protected]",
  "metadata": {
    "createdAt": "2017-03-17T01:34:03.000Z",
    "lastSignedInAt": "2017-03-17T01:34:03.000Z"
  },
  "photoURL": "https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100/REDACTED",
  "providerData": [
    {
      "displayName": "Robert-Jan Huijsman",
      "email": "[email protected]",
      "photoURL": "https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100/REDACTED",
      "providerId": "facebook.com",
      "uid": "http://facebook.com/1234567890"
    }
  ],
  "uid": "AaBbCcDdEeFf"
}
like image 51
Robert-Jan Huijsman Avatar answered Nov 13 '22 06:11

Robert-Jan Huijsman