Firebase Functions Tutorial #9 - Creating User Records

Net Ninja · Beginner ·🔧 Backend Engineering ·6y ago

Key Takeaways

Creates a user record in Firestore using a cloud function triggered by authentication

Full Transcript

other than gangs so in the last video we created two more cloud functions and they were invoked based on an authentication trigger either creating the user or deleting a user now that was fine but it did give us these errors function returned undefined expected promise old value and that's because we're not actually returning a value and normally we need to return a value or promise for background trigger functions and have created these comments just to remind us to do that in these functions so eventually we will do that we will get rid of those errors but also I want to do a bit of extra work inside these cloud functions because at the minute all they're doing is logging this stuff to the console and that's kind of pointless what I actually want to do inside this function where a new use is created is create a record for that user in the firestore database and then inside that collection a user's collection inside that database we can have a record for this user which stores additional information for that user because firebase Earth doesn't store additional information about users it only stores the bare essentials their email their password etc but what if I want to store I don't know a list of hobbies for a user or a biography or something else then I need to store that in the database so that's what we're going to do create a new database record inside a user's collection for every user that is created so first of all let me get rid of this console log we don't need that anymore and I'll do the same thing down here and by the way we'll do the opposite right here we'll delete the record for the user when they are deleted themselves now if we want to interact with the firestore database inside our cloud functions we need to require something at the top and that is the admin SDK and the admin SDK allows us basically to interact with the firestore and other things so I'm going to say Const admin is equal to require and inside firebase - admin ok so now we have that we need to say admin dot initialize up like so and initializes the app and allows us to do all of this communication directly from this admin ok so what I'm gonna say down here is admin dot firestore which we have access to and by the way if you're unfamiliar with firestore I would recommend and check it out my firestore tutorial series first of all because I'm not going to go into great depth about how everything works with firestore I wouldn't expect you to know the basics already so we're accessing the firestore right here then we want to access a specific collection inside that fire store and that collection is going to be users now you might be thinking well we've not created a user's collection yet we don't have any collections in the fire store yet but it doesn't matter because when we come to grab a collection and a document inside that collection if the collection doesn't already exist firebase will create it for us which is nice so inside that users collection I want to get a document reference and I'm going to pass in the UID of the user that just signed up so if I say Hugh's her don't you ID it's gonna look for a document that has this ID ok now at the minute that document doesn't exist but again that doesn't matter if it doesn't exist then it's going to create it for us so it will create a document with the UID of the user and that way our user is going to have the same UID as the user record so it's kind of linking firebase earth and our fire store record for that user together so we're creating this document with this UID and then we want to set some values of that document so we use the sets method to do that and we pass in an object and we set different key value pairs so say for example I want to store the email I don't have to because that's being stored on firebase off but just to make up some values let's do it because we have access to that on the user object that we get right here so I can take that user access the email property and that is going to store that now in the email property of this document inside the firestore collection users inside this document so that's the first property next I'm going to create a new property and that's called up voted on and that will be an empty array to begin with now what is this property all about well eventually if we take a look at the project you can see right here we have arrows and I can upvote different tutorials now this array right here is going to keep track of which tutorials this particular user has voted so this information this is not stored on firebase authentication we can't do that we can't store it in a fire store for that user and that's what we're doing so to begin with it's going to be empty but later on in the series as we click on those arrows to upvotes it's going to add the different requests that this user has voted on and keep track of those ok so there we go that's done that's all we wanted to do in here we're creating now a user record for this user now remember for background triggers you must return a value or a promise so this right here well this is asynchronous and it returns a promise so all I have to do is place the return keyword explicitly before that and it's now going to return the promise returned by this action makes sense cool so let's do the same thing down here I'm going to delete that spare line and paste this in but now I don't want to set this instead I want to delete this document so what I'm going to do is instead of returning right here I'm going to say Const and then I'm going to say the document is equal to add mint fire store collection dot doc we still want the document of the current user that's been deleted and instead of set we'll get rid of that we want to delete it so we've stored a reference to that document now inside doc and all I have to do is return doc and then use a method on this called deletes that goes out it deletes it from the firestore database and it returns a promise therefore we can explicitly return up right here and this now should all work so if I save this I'm gonna deploy those and then we'll test them out now while that's happening I'm actually gonna go over here to our authentication tab and I'm going to delete the current user so we can start from a blank slate so that now in the future when we create a user we create a record for them and then when we delete the user we delete that record so we're starting from a blank slate now and let's see if this has deployed successfully it looks like it has done but let's go to our functions over here the dashboard and I'm just gonna refresh just to make sure they're both still there okay cool and now the first thing I'll do is go and create a user from the front-end so let me refresh over here and we shouldn't be logged in anymore but let's sign out anyway so let's register a new user and this user is going to be shown that's the net oops let me spell this correctly the net ninja code at UK and the password is going to be test one two three so register make sure it works on the front end first of all it did and then if we go over to firebase we need to go to the database but let me just refresh over here to make sure we have that user as well created in firebase authentication okay we have to I forgot to delete that one in the last tutorial I'll do that in a second but now first let me go to the fire store we can see now we have a user's collection it created that for us and we should have a single document inside here which represents that user that just signed up that just registered and we can see there's the email and this is the up voted on array which is empty at the minute so it created that user record for us and look at this this ID right here it starts in is and ends in b2 this should be the same is the ID that firebase Earth created for us so there's kind of like a synergy between those two they're linked up and down here we have this one peach now I am gonna delete that because we don't actually have a record for her so delete account it's probably going to try to delete that document over here which corresponds to that account but it doesn't exist so it's not going to be able to do that but now if I go ahead and delete this user so let me go back over here let me delete this user like so it should then go ahead and try to delete this which it does as well so now this is all in sync whenever we create a new user it creates a document for us inside the users collection for that user and whenever we delete that user it deletes that document for us as well so end let me just create that user one more time so we have something to work with in future tutorials so Shawn that's the net ninja code at UK and test123 register and hopefully now we should get that document back over here yes we do so there we go Shawn at the net ninja and if we go to the off tab and refresh just to make sure we have that one single user yep there we go awesome all working so there's a good example of why we'd use authentication triggers in our firebase projects in the next video we're going to move on and we're going to create a function to handle the adding of new requests

Original Description

Hey gang, in this tutorial we'll use a cloud function to create a new user record in thew firestore database whenever they sign up. 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: https://github.com/iamshaunjp/firebase-functions/tree/lesson-2 🐱‍💻 🐱‍💻 Other Related Free Courses: + Firebase Firestore - https://www.youtube.com/playlist?list=PL4cUxeGkcC9itfjle0ji1xOZ2cjRGY_WB + Firebase Authentication - https://www.youtube.com/playlist?list=PL4cUxeGkcC9jUPIes_B8vRjn1_GaplOPQ + Firebase Hosting - https://www.youtube.com/playlist?list=PL4cUxeGkcC9he0kHAyiyr3dDO2xw0NWoP 🐱‍💻 🐱‍💻 The Net Ninja Community Boards: https://community.thenetninja.co.uk/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

📰
I built a native Android app in an afternoon, and I've never written a line of Kotlin
Learn how to build a native Android app without prior Kotlin knowledge, leveraging modern tools and frameworks to streamline development
Dev.to · Tilde A. Thurium
📰
Vibe Coding Is Real Now — Here’s How to Do It Without Wrecking Your Codebase
Learn how to apply vibe coding effectively to speed up feature development without compromising code quality, and why discipline is key to its success
Medium · Programming
📰
How to build your first MCP server in 10 minutes
Learn to build a Minecraft server using MCP in under 10 minutes for a seamless gaming experience
Dev.to · GrahamduesCN
📰
Revisiting My Software Engineering Journey
Learn how browsers communicate with servers using HTTP and HTTPS protocols, essential for software engineers to build scalable web applications
Medium · JavaScript
Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →