Cloud Functions for Firebase Auth Events for Social Provider

An attempt to update user data in the Realtime database when creating a User. 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 the user is created through. Facebook, provider data is not 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 at creation?

+5
source share
1 answer

You do not need to make another call to admin.auth().getUser(...) . Conveniently, the event.data that you get in this function is already UserRecord !

Here is my very simple code for printing Auth events:

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

And here is what it comes out when I log in via Facebook:

 { "displayName": "Robert-Jan Huijsman", "email": " REDACTED@gmail.com ", "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": " REDACTED@gmail.com ", "photoURL": "https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100/REDACTED", "providerId": "facebook.com", "uid": "http://facebook.com/1234567890" } ], "uid": "AaBbCcDdEeFf" } 
+1
source

Source: https://habr.com/ru/post/1265577/


All Articles