Skip to content

Instantly share code, notes, and snippets.

@birchb
Last active April 5, 2020 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save birchb/4332e064f3637312d571814540968ebe to your computer and use it in GitHub Desktop.
Save birchb/4332e064f3637312d571814540968ebe to your computer and use it in GitHub Desktop.
// This is the firebase script called to create each user.
exports.createUser = functions.https.onCall((data) => {
const user = data.user[0]
let displayName = `${user.firstName} ${user.lastName}`
return admin.auth().createUser({
displayName: displayName,
email: user.email,
emailVerified: false,
password: user.password
})
// the createUser firebase function returns a userRecord.
.then(function (userRecord) {
// create a reference to a new document in the user collection with the user's uid.
let userRef = admin.firestore().collection('users').doc(userRecord.uid)
let userObj = {
// * This creates the actual user record in the users collection.
active: true,
displayName: userRecord.displayName,
email: user.email,
emailVerified: false,
firstName: user.firstName,
fullName: `${user.lastName}, ${user.firstName}`,
lastName: user.lastName,
photoURL: '',
role: user.role,
uid: userRecord.uid
}
// I use each new user's institutional email address to assign an institution record. It uses the next to last word in their email address.
let array = user.email.split('@')
userObj.username = array[0]
userObj.institution = array[1].split('.').slice(-2).join('.')
return userRef.set(userObj)
.then(() => {
return { text: `Successfully created new user: ${userRecord.displayName}` } // successMessage in the other script.
})
.catch(error => {
console.log(error)
})
})
.catch(function (error) {
// return { text: `Error creating new user ${user.email}:${errorMessage}` } // errorMessage in the other script.
return { text: `${error}` }
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment