Firebase Functions Tutorial #9 - Creating User Records
Skills:
Backend Performance60%
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: Backend Performance
View skill →Related Reads
📰
📰
📰
📰
I built a native Android app in an afternoon, and I've never written a line of Kotlin
Dev.to · Tilde A. Thurium
Vibe Coding Is Real Now — Here’s How to Do It Without Wrecking Your Codebase
Medium · Programming
How to build your first MCP server in 10 minutes
Dev.to · GrahamduesCN
Revisiting My Software Engineering Journey
Medium · JavaScript
🎓
Tutor Explanation
DeepCamp AI