问题
I have a cloud function for onCreate that looks like
exports.addNewUserToCollection = functions.auth.user().onCreate(event => {
const user = event.data; // The Firebase user.
var userData = JSON.parse(JSON.stringify(user));
if (!userData.displayName){
userData.displayName = '(no name)'
}
return db
.collection("users")
.doc(user.uid)
.set(userData);
});
it works fine except when the user signs up via email.
When the user signs up via email they are prompted for a first and last name and this information makes it into the authentication data.
I know this because inside their session I can call getCurrentUser() and retrieve their displayName property.
The event data in the code above however does not contain displayName (or first and last name for that matter)
What gives?
回答1:
When you create an email+password account in Firebase Authentication, you specify only the minimum information: email and password. For example on iOS this is:
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
// ...
}
Your Cloud Function is triggered with this precise information: just the email and password.
While it is possible to later update the user profile to include the display name, that information is not passed on to the Cloud Function trigger (which currently only triggers on account creation).
Possible workaround are:
- Call a Cloud Function from your code, when you update the user profile to set the display name.
- Hand the entire user creation of to Cloud Functions, passing email+password+displayName into your custom function, which then creates the user account, sets their display name, and creates the document for that user.
来源:https://stackoverflow.com/questions/48374855/firebase-cloud-function-authentication-oncreate-event-doesnt-contain-displayna