5

Cố gắng cập nhật dữ liệu Người dùng trong Cơ sở dữ liệu thời gian thực khi Người dùng được tạo. Đây là mã của tôi:Chức năng đám mây cho sự kiện xác thực Firebase dành cho nhà cung cấp dịch vụ xã hội

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; 
}); 

Tuy nhiên, khi người dùng được tạo qua. Facebook, dữ liệu của nhà cung cấp không được cung cấp. Đây là nhật ký bảng điều khiển:

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: [] } 

Tôi có làm gì sai hoặc dữ liệu này không được cung cấp khi tạo?

+0

Bạn đã bao giờ tìm thấy một lời giải thích, là tại sao các người dùng chưa hoàn thành cho Facebook và Google? –

Trả lời

1

Bạn không cần thực hiện một cuộc gọi khác tới admin.auth().getUser(...). Thuận tiện, số event.data mà bạn nhận được trong chức năng này đã là UserRecord!

Đây là mã rất đơn giản của tôi để in Auth sự kiện:

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

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

Và đây là những gì mà kết quả đầu ra khi tôi đăng nhập qua 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" 
} 
Các vấn đề liên quan