Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,180 --> 00:00:08,189
OK, guys, so now that we have our Google Sinon, we have to actually figure out how to store this
2
00:00:08,189 --> 00:00:16,440
authenticated user into our database, because right now the authenticated user that we're getting back
3
00:00:16,440 --> 00:00:21,590
from our off library is not actually something in our database.
4
00:00:22,650 --> 00:00:27,720
So when we actually sign in with our Google sign in.
5
00:00:29,370 --> 00:00:37,410
What we get back is this user authentication object that has a lot of properties on it that we don't
6
00:00:37,410 --> 00:00:38,400
actually want.
7
00:00:39,000 --> 00:00:44,130
So there are so many properties on this because a lot of these are related to things that you might
8
00:00:44,130 --> 00:00:49,520
want to do with a Google auth object from Google's own API.
9
00:00:49,530 --> 00:00:55,830
So this is supposed to integrate with many of their other services or what they offer in their Google
10
00:00:55,830 --> 00:00:58,850
Sinon to other platforms, other libraries.
11
00:00:59,340 --> 00:01:03,780
It's all unrelated to what we need in our application.
12
00:01:04,810 --> 00:01:12,730
In fact, the only things that we really need in our application is this display name, this email.
13
00:01:13,710 --> 00:01:22,380
And possibly, maybe a phone number, right, but the most important thing is this Eweida this UUID
14
00:01:22,380 --> 00:01:29,610
represents the dynamically generated I.D. string that Google made for us when we authenticated this
15
00:01:29,610 --> 00:01:31,570
user using the Google sign it.
16
00:01:32,190 --> 00:01:38,730
In fact, if we go into our authentication and we look at our user inside of our off tab, whenever
17
00:01:38,730 --> 00:01:43,410
a user signs in to our application using the off library that we have.
18
00:01:44,480 --> 00:01:52,670
Regardless of if it's email, Sinon or Google sign in, we will get this user right and this user has
19
00:01:52,670 --> 00:01:53,770
this UUID.
20
00:01:54,740 --> 00:01:57,980
We have to take this user off object.
21
00:01:59,000 --> 00:02:05,240
Figure out the properties that we want from it and put it into our database.
22
00:02:06,410 --> 00:02:11,060
Particularly, what we want to do is put it into our users collection.
23
00:02:12,130 --> 00:02:20,050
Now we're going to write some code that will allow us to do this, as well as understanding what the
24
00:02:20,050 --> 00:02:28,480
Firestorm Library actually does for us, because there's actually certain rules or certain things that
25
00:02:28,480 --> 00:02:32,980
we have to abide by in order to use and update the fire store database.
26
00:02:33,460 --> 00:02:36,090
And I'll explain them as we write out this function.
27
00:02:36,880 --> 00:02:43,180
So the function that we're trying to write is going to be one that allows us to take that user off object
28
00:02:43,180 --> 00:02:48,950
that we got back from our authentication library and then store inside of our database.
29
00:02:49,720 --> 00:02:55,620
So what we're going to call it is this function called create user profile document.
30
00:02:55,900 --> 00:02:56,290
Right.
31
00:02:56,830 --> 00:03:02,650
And this is going to be an asynchronous thing because we're making an API request.
32
00:03:02,650 --> 00:03:02,920
Right.
33
00:03:02,930 --> 00:03:04,450
That's an asynchronous action.
34
00:03:05,870 --> 00:03:07,580
So we're going to call async.
35
00:03:09,040 --> 00:03:15,490
And what we're going to pass into it is, of course, our user object, right, that we get back from
36
00:03:15,490 --> 00:03:19,250
the library, that's that same object that we were logging right here.
37
00:03:19,420 --> 00:03:20,650
We're going to pass that in.
38
00:03:21,780 --> 00:03:29,940
And then we're also going to pass in possibly any additional data that we might need, so this could
39
00:03:29,940 --> 00:03:34,530
be something that we're going to use later when we do our sign up.
40
00:03:34,530 --> 00:03:39,870
But for now, we're just going to say we also might want some additional data that will come in in the
41
00:03:39,870 --> 00:03:40,980
format of an object.
42
00:03:41,130 --> 00:03:43,050
And I'll show you what we'll do with this additional data.
43
00:03:43,890 --> 00:03:44,280
Now.
44
00:03:45,540 --> 00:03:51,870
As we saw, when our user signs out, we actually get back nul from this library, right?
45
00:03:51,930 --> 00:03:55,050
If I were to sign out right now, we'll see that we log nul.
46
00:03:55,560 --> 00:04:05,160
So we only want to perform this save to our database if we get back a user off object, which means
47
00:04:05,160 --> 00:04:07,900
that they signed in, not that they signed out.
48
00:04:08,250 --> 00:04:15,200
So our function has to make sure that we're checking if we're actually getting back a valid object.
49
00:04:15,900 --> 00:04:21,600
So inside of our code, what we're going to do is we're going to say if there is no user off, right.
50
00:04:22,380 --> 00:04:24,300
Because this will evaluate null.
51
00:04:24,450 --> 00:04:24,810
Right.
52
00:04:24,960 --> 00:04:26,670
And if it's null, then it's false.
53
00:04:27,150 --> 00:04:29,690
And with the bang, we're going to say we want to reverse it.
54
00:04:29,730 --> 00:04:31,930
So if it's not false.
55
00:04:31,950 --> 00:04:32,280
Right.
56
00:04:32,910 --> 00:04:38,850
So I know that context is confusing, but this is essentially a saying if the user auth object does
57
00:04:38,850 --> 00:04:41,660
not exist, then we want to return from this function.
58
00:04:41,940 --> 00:04:43,230
We don't want to do anything else.
59
00:04:43,230 --> 00:04:44,790
We want to exit from this function.
60
00:04:46,640 --> 00:04:53,630
If it does exist, though, what we're going to do is we are going to query inside a fire store for
61
00:04:53,630 --> 00:04:56,510
the document to see if it already exists.
62
00:04:57,230 --> 00:05:02,000
Now, in order for us to do this, we have to understand what it is that we're going to get back.
63
00:05:03,610 --> 00:05:12,790
Now, inside of here, I have a actual slide for us to understand, the Firestorm Library gives us back
64
00:05:13,120 --> 00:05:17,290
one of two different types of potential objects.
65
00:05:17,890 --> 00:05:22,560
We either get back a query reference or a query snapshot.
66
00:05:23,020 --> 00:05:26,300
They're both different objects that do different things in firestorm.
67
00:05:26,830 --> 00:05:29,470
But let's talk about what a query is first.
68
00:05:30,040 --> 00:05:36,040
A query is simply us asking Firestar for some document or collection.
69
00:05:37,000 --> 00:05:43,480
If you remember from the last lesson where we talked about Firestorm, we called dot, dot or dot collection
70
00:05:43,480 --> 00:05:47,200
and then passed that string to the location inside of the database.
71
00:05:47,590 --> 00:05:47,950
Right.
72
00:05:48,160 --> 00:05:55,300
For either collection or a document, that act of asking is what a query is.
73
00:05:56,330 --> 00:06:03,530
And what we'll get back when we query is a query reference, and it can either come in the format of
74
00:06:03,530 --> 00:06:08,000
a document or a collection if we're praying for a document or collection, respectively.
75
00:06:09,360 --> 00:06:15,930
A firestorm will always return us these objects, whether it be the reference or the snapshot, which
76
00:06:15,930 --> 00:06:23,280
we'll talk about later, Firestorm will give us the object because using the object, we can determine
77
00:06:23,280 --> 00:06:26,110
whether or not there's any data there.
78
00:06:27,060 --> 00:06:35,910
Now, the reference is simply a object that represents that current place in the database that we're
79
00:06:35,910 --> 00:06:36,640
asking for.
80
00:06:37,230 --> 00:06:38,610
So let's actually try it out.
81
00:06:38,610 --> 00:06:42,680
And we can call it either using DOT Doc passing in the collections this way.
82
00:06:42,690 --> 00:06:42,860
Right.
83
00:06:42,930 --> 00:06:46,930
Or DOT collections and passing in the path to that collection.
84
00:06:47,370 --> 00:06:51,140
We explore that earlier in a previous lesson, but let's try it out.
85
00:06:53,130 --> 00:07:00,510
What we're going to do here is we are simply going to console log the firestorm and then calling for
86
00:07:00,510 --> 00:07:07,230
the dock users and then a random user I.D., one that we definitely know doesn't exist.
87
00:07:07,230 --> 00:07:09,510
Right, because our collection is empty anyways.
88
00:07:10,560 --> 00:07:18,930
If we were to save this and pass this into our apgar's right, we import it in and we use it whenever
89
00:07:18,930 --> 00:07:20,430
we get back and off object.
90
00:07:22,200 --> 00:07:27,540
We know that this will be a asynchronous because we're going to, of course, make a potential API request
91
00:07:27,570 --> 00:07:29,490
right, to our fire store.
92
00:07:30,540 --> 00:07:33,990
And then what we'll do is instead of setting the stage here.
93
00:07:35,390 --> 00:07:41,540
We are going to console oh, wait, sorry, we're just going to fire this using our user off object
94
00:07:41,570 --> 00:07:44,270
that we know that we're getting back from our off library.
95
00:07:45,680 --> 00:07:51,470
If we were to save, we go back to our application, we see that we're getting back this document reference
96
00:07:51,470 --> 00:07:51,890
object.
97
00:07:53,090 --> 00:07:56,900
We also see that the random I.D. that we pass to it is here.
98
00:07:58,080 --> 00:08:03,990
We see that we get the path that we also passed in, right, so we know where the current location of
99
00:08:03,990 --> 00:08:06,270
this document reference is.
100
00:08:07,290 --> 00:08:12,620
But we are always getting back this object, even though it doesn't exist inside of a database.
101
00:08:13,470 --> 00:08:14,430
So why is that?
102
00:08:15,330 --> 00:08:23,370
Well, the reason is because we actually use this reference object to tell Firestar whether to save
103
00:08:23,370 --> 00:08:30,600
data to this place inside of our database or to get data from this location in the database.
104
00:08:32,159 --> 00:08:35,580
It does not have the actual data related to the document.
105
00:08:36,299 --> 00:08:44,610
Instead, it gives us properties that tells details about it, like the I.D. and the path to this specific
106
00:08:44,610 --> 00:08:46,300
query reference, as we already saw.
107
00:08:47,190 --> 00:08:51,690
But what we're also able to see if this is a query reference, right.
108
00:08:51,690 --> 00:09:00,150
Meaning it's a document reference, is that the parent is a collection reference representing the collection
109
00:09:00,150 --> 00:09:01,480
that this document is in.
110
00:09:02,070 --> 00:09:08,100
So here we'll see in this collection reference that we have our idea of users and the path is just users.
111
00:09:09,870 --> 00:09:14,520
Now, the difference between the document reference and the collection reference is that the document
112
00:09:14,520 --> 00:09:23,850
reference object we used to perform are crude methods on our Firestar crud being either creating, retrieving,
113
00:09:24,030 --> 00:09:29,850
updating or deleting the data in that place, that location.
114
00:09:31,330 --> 00:09:41,470
Now, when we actually retrieve the data, right, we're calling DOT get right and don't get is us pulling
115
00:09:41,470 --> 00:09:45,940
out a snapshot object of that place in the database.
116
00:09:47,860 --> 00:09:52,390
And that snapshot object was that second type of object that I was telling you that faster could give
117
00:09:52,390 --> 00:09:59,830
us back, we'll actually use it and we'll be able to use that snapshot object in order for us to determine
118
00:09:59,860 --> 00:10:07,160
with our code whether or not there's any data in this current reference in our fire store.
119
00:10:07,510 --> 00:10:08,920
So I'll show you what that is as well.
120
00:10:09,580 --> 00:10:14,290
But again, the snapshot object we get back from calling doGet, right?
121
00:10:14,300 --> 00:10:16,360
So we either call document refought get.
122
00:10:17,250 --> 00:10:23,010
Or we call collection rough, like a collection ref also gives us back a snapshot of the collection,
123
00:10:23,400 --> 00:10:26,270
but it's different from what we're going to use in our current code.
124
00:10:26,730 --> 00:10:30,170
But it's just good to know that this is something that you might possibly have to do.
125
00:10:30,900 --> 00:10:34,570
And of course, from the document, we're going to get a document snapshot.
126
00:10:34,920 --> 00:10:36,690
So let's actually take a look at that.
127
00:10:37,740 --> 00:10:45,000
So back in our code, what we're going to do here is we are going to move this into a contest of some
128
00:10:45,000 --> 00:10:45,270
kind.
129
00:10:45,300 --> 00:10:45,540
Right.
130
00:10:45,580 --> 00:10:50,850
So consed user ref equals fast or dot doc getting that user.
131
00:10:51,360 --> 00:10:54,990
And then I'm going to hear say, how do we get the snapshot.
132
00:10:55,890 --> 00:10:58,800
Well it's going to be snapshot equals user raef.
133
00:10:58,800 --> 00:10:59,090
Right.
134
00:10:59,100 --> 00:11:02,040
So we have our user reference dot get.
135
00:11:04,390 --> 00:11:07,840
Now, this is an async, right, so we're going to await this.
136
00:11:09,280 --> 00:11:14,340
And then we're just going to log the snapshot, so we take a look at what it is that we're getting now,
137
00:11:15,130 --> 00:11:17,530
we see that this is our document snapshot object.
138
00:11:17,530 --> 00:11:20,860
And on it is this property exists.
139
00:11:21,790 --> 00:11:26,890
The exists property tells us whether or not there's any data there.
140
00:11:28,030 --> 00:11:32,140
The I.D. tells us, of course, the idea of the same document.
141
00:11:33,380 --> 00:11:38,450
Metadata gives us certain information about things like when it was created, right, whether or not
142
00:11:38,450 --> 00:11:43,010
it's cached at whether or not there's pending rights, meaning if there's anything that needs to be
143
00:11:43,010 --> 00:11:48,680
updated right to the snapshot, don't worry about this for now, but just know that this is something
144
00:11:48,680 --> 00:11:52,640
that you might want to use if you get really, really deep with Firestorm.
145
00:11:54,680 --> 00:11:59,690
And then, of course, we can also always reference back to the document reference object that we queried
146
00:11:59,690 --> 00:12:00,650
our snapshot from.
147
00:12:01,570 --> 00:12:09,220
Now, instead of using this fake one, what we want to use is the user off UID right, because we now
148
00:12:09,220 --> 00:12:15,070
want to see whether or not the user off object that we get back from authentication library actually
149
00:12:15,100 --> 00:12:16,940
already exists in our database.
150
00:12:17,650 --> 00:12:25,270
So here we're able to say instead of using an actual string, what you string interpolation, I will
151
00:12:25,270 --> 00:12:29,590
say users at our user off dot uid.
152
00:12:30,910 --> 00:12:39,880
We will get back the user reference at that location and then we'll get a snapshot and using that snapshot,
153
00:12:39,890 --> 00:12:42,830
we're going to figure out whether or not there is data there, right.
154
00:12:42,910 --> 00:12:47,500
Whether or not we've already stored this user object that we've authenticated.
155
00:12:48,630 --> 00:12:52,210
And here we'll see that the existence is false, of course, right?
156
00:12:52,230 --> 00:12:56,700
But the idea is now that it that we were getting back from our awful library.
157
00:12:58,270 --> 00:13:04,510
So what we're going to say is we're going to check inside of our code, right, using that snapshot.
158
00:13:05,970 --> 00:13:07,110
Exists property.
159
00:13:09,030 --> 00:13:16,950
So if it doesn't exist, we want to actually create a piece of data there and we're going to create
160
00:13:16,950 --> 00:13:25,320
it using, argues Şeref, because if you remember from our slide, in order for us to create, we have
161
00:13:25,320 --> 00:13:28,500
to use the document reference object, not the snapshot.
162
00:13:28,920 --> 00:13:31,010
The snapshot simply represents the data.
163
00:13:31,740 --> 00:13:38,270
Any methods of creating, getting, updating or deleting that data.
164
00:13:39,000 --> 00:13:41,820
We have to use the document reference object for.
165
00:13:43,660 --> 00:13:50,830
So here, what we're going to do before we create it is we need to see what properties we actually want
166
00:13:50,830 --> 00:13:51,950
to store, right.
167
00:13:51,970 --> 00:13:57,670
What are the data that we want to use in order to create this actual document?
168
00:13:58,660 --> 00:14:01,390
So what we want is the display name.
169
00:14:03,800 --> 00:14:10,280
And the email from our user off, right, as we remember from that giant object that we first logged,
170
00:14:10,550 --> 00:14:11,630
we want the display name.
171
00:14:11,840 --> 00:14:12,680
We want the email.
172
00:14:14,100 --> 00:14:21,160
And we also want to make sure that we know inside of our database when we made that document.
173
00:14:21,810 --> 00:14:27,570
So by calling New Date, we're creating a new JavaScript data object that tells us the current date
174
00:14:27,570 --> 00:14:29,730
at the current time when this was invoked.
175
00:14:31,610 --> 00:14:36,950
And what we're going to do is we're going to wrap this inside of a try catch block.
176
00:14:38,580 --> 00:14:44,370
Because what we're going to do is an asynchronous request to our database to actually store this data
177
00:14:44,370 --> 00:14:44,620
now.
178
00:14:45,480 --> 00:14:50,160
So what we're going to do is we're going to await our user ref calling dot set.
179
00:14:51,290 --> 00:14:55,700
Dots, that being the create method, and we're going to make an object, right, we're going to pass
180
00:14:55,700 --> 00:15:02,780
on an object where we have the display name, we have the email, we have the created up, and we're
181
00:15:02,780 --> 00:15:06,410
also spreading in any other additional data that we might want.
182
00:15:06,740 --> 00:15:11,450
So as you remember I mentioned earlier, we're going to pass this additional data into this function
183
00:15:11,480 --> 00:15:12,710
as an object.
184
00:15:14,140 --> 00:15:20,770
If this has any issues, we're going to console log error, creating user right and when they're work
185
00:15:20,860 --> 00:15:22,890
to log out the error message as well.
186
00:15:25,720 --> 00:15:32,470
And if this snapshot doesn't exist now will create data in that place.
187
00:15:32,590 --> 00:15:34,000
That's all this code is doing.
188
00:15:34,900 --> 00:15:38,140
We're checking if there's any data in that place.
189
00:15:38,650 --> 00:15:44,050
If there isn't, create a new user using the data from our user auth object.
190
00:15:44,910 --> 00:15:50,150
Now, what this function will also do is always return us back our user ref.
191
00:15:51,230 --> 00:15:56,330
Because there's a chance that we want to use this user reference, object to do other things, write
192
00:15:56,870 --> 00:16:03,050
this code simply creates the snapshot, it creates the data, but we might still want to use a reference
193
00:16:03,050 --> 00:16:04,710
in our code for something.
194
00:16:05,390 --> 00:16:07,270
So that's why we're going to return it from this user.
195
00:16:08,090 --> 00:16:11,870
And now when we call to create user profile document.
196
00:16:12,230 --> 00:16:18,110
Right, our opinion realized, you see, we didn't log, so we probably created something in our database
197
00:16:18,110 --> 00:16:21,920
and we check there is our user in our database.
198
00:16:23,900 --> 00:16:31,670
Our current user is now being stored, and if we refresh, as we know, are functional for every time
199
00:16:31,670 --> 00:16:34,220
because of is always persisting.
200
00:16:36,150 --> 00:16:42,240
Despite that, we've only still got the one user, we're not making multiple copies because our code,
201
00:16:42,240 --> 00:16:47,140
again, checks if the snapshot at this place exists or not.
202
00:16:48,000 --> 00:16:49,340
So that's what our code is doing.
203
00:16:49,710 --> 00:16:57,150
We're checking and now we're making these new users inside of our database based on that user off object.
204
00:16:57,630 --> 00:17:03,540
And now that we have these objects being stored in our database and being pulled into our application,
205
00:17:04,079 --> 00:17:09,000
let's actually figure out how to store this data so that we can use it in our application.
20904
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.