Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,700 --> 00:00:08,380
Now that we have our sign in with Google Button, we actually have no way of knowing inside of our application
2
00:00:08,380 --> 00:00:14,590
whether or not our sign was successful with Google, but we can tell that our user was added to our
3
00:00:14,590 --> 00:00:22,660
firebase and authenticated when we go back to our firebase console in our authentication, if we click,
4
00:00:22,660 --> 00:00:26,610
this user's tab will see this list of users.
5
00:00:26,620 --> 00:00:33,100
And right there, that user is the one that we just logged in with, with our sign and with Google.
6
00:00:34,190 --> 00:00:38,270
So we know that it's working and we're getting authenticated.
7
00:00:39,500 --> 00:00:45,980
Now, how do we actually add this into our application so that our application is aware that those changes
8
00:00:45,980 --> 00:00:50,510
are happening while Firebase gives us a really easy way to do that?
9
00:00:51,350 --> 00:00:55,090
So let's go back to our application and let's go to our app, Abcess.
10
00:00:56,030 --> 00:00:58,940
Inside of our abcess, what we want to do.
11
00:00:59,960 --> 00:01:05,930
Is we want to first import in off from our firebase utils.
12
00:01:07,760 --> 00:01:09,220
So let's import off.
13
00:01:12,180 --> 00:01:20,820
Firebase Dasch, Firebase Utils, and what we want to write is we want to actually store the state of
14
00:01:20,820 --> 00:01:23,190
our user in our app, right.
15
00:01:23,220 --> 00:01:29,610
So when a user logs in using whatever service we use, whether it's Google Sinon or email and password,
16
00:01:30,000 --> 00:01:36,180
we want to store that on the App State so that we can pass it into components that need it because we
17
00:01:36,180 --> 00:01:40,060
want to access our current user object throughout our application.
18
00:01:40,830 --> 00:01:47,850
So what we'll do is we'll convert our app over to a class component so we can have access to state.
19
00:01:52,440 --> 00:01:56,190
And we'll put this return inside of our render.
20
00:01:59,180 --> 00:02:01,340
And now everything will work the same.
21
00:02:03,170 --> 00:02:13,010
We'll just add our constructor, call our super and call this Staats state equals an object where current
22
00:02:13,010 --> 00:02:14,870
user is null.
23
00:02:16,190 --> 00:02:23,910
To properly understand what Firebase Auth gives us, we have to understand the concept of a subscription.
24
00:02:24,680 --> 00:02:32,390
So normally how we fetch data before is we inside of our component did mount.
25
00:02:32,390 --> 00:02:35,690
Method component did mount.
26
00:02:37,170 --> 00:02:44,480
We're used to firing a fair shot to the back end to fetch data, but this is only a one off thing.
27
00:02:44,880 --> 00:02:51,000
Once the code calls fetch, it won't come back again until a component did Mount Life-cycle method gets
28
00:02:51,000 --> 00:02:51,680
called again.
29
00:02:52,290 --> 00:02:55,520
But we don't want to remount our app.
30
00:02:55,560 --> 00:03:02,880
We just want to always know when Firebase has realized that the authentication state has changed.
31
00:03:02,880 --> 00:03:10,410
So whenever somebody signs in, whenever somebody signs out, we want to be aware of that change without
32
00:03:10,410 --> 00:03:11,940
having to manually fetch.
33
00:03:11,940 --> 00:03:14,970
Firebase actually gives us that really easily.
34
00:03:15,700 --> 00:03:21,660
So when we call off on off state changed.
35
00:03:23,270 --> 00:03:32,000
This is a method on the off library that we get from Firebase, right, Firebase off and then this off.
36
00:03:34,100 --> 00:03:44,090
Inside, what it takes is a function where the parameter is what the user state is of the off on our
37
00:03:44,090 --> 00:03:51,230
firebase project and our callback inside, we're going to call this dot set state where we're setting
38
00:03:51,230 --> 00:03:54,560
current user to that user object.
39
00:03:56,060 --> 00:03:59,420
So let's log it out as well just to see what it looks like.
40
00:04:00,760 --> 00:04:05,440
Now, when we go back to our application and we open up our consul.
41
00:04:06,360 --> 00:04:13,650
We actually see that this is our user, the one that we just signed in with with Google, so because
42
00:04:13,650 --> 00:04:19,500
our application earlier when we selected signed with Google, it's already stored that user and it knows
43
00:04:19,500 --> 00:04:24,150
that this application that's making that request has not yet signed out.
44
00:04:24,150 --> 00:04:30,270
So that's why we're still seeing the object, even though our app refreshed when we saved our app just
45
00:04:30,300 --> 00:04:33,810
now, Firebase is still aware that this was the last user.
46
00:04:33,810 --> 00:04:34,100
Right.
47
00:04:34,140 --> 00:04:40,200
That got signed in with and this is a really cool feature that we get from Firebase right out the box.
48
00:04:40,740 --> 00:04:43,680
User authenticated session persistance.
49
00:04:44,310 --> 00:04:50,040
This is actually a setting that you can change in Firebase, which we will definitely explore in a later
50
00:04:50,040 --> 00:04:50,580
lesson.
51
00:04:50,580 --> 00:04:56,940
But right now, I want to focus on the basics of Firebase because there's so much for us just to understand
52
00:04:56,940 --> 00:04:58,410
what Firebase can do for us.
53
00:04:59,380 --> 00:05:07,090
But this feature is awesome because if you think about it, we don't actually want our user to have
54
00:05:07,090 --> 00:05:10,780
to resign in whatever we either refresh our page.
55
00:05:11,770 --> 00:05:17,020
Where we see we're still getting now our user or if we close the window, right, without ever signing
56
00:05:17,020 --> 00:05:19,390
out and then come back to our application.
57
00:05:21,250 --> 00:05:27,370
And again, we get that user object from Firebase, which is great for us, because then we can decide
58
00:05:27,370 --> 00:05:28,510
what to do with it, right?
59
00:05:28,510 --> 00:05:35,170
Because we know that our user is still the same and firebases aware because it keeps track of all the
60
00:05:35,170 --> 00:05:39,100
instances of the application that are open and communicating with it.
61
00:05:39,370 --> 00:05:45,370
So it knows that, OK, a user came from this instance of the application before and they never signed
62
00:05:45,370 --> 00:05:45,710
out.
63
00:05:45,730 --> 00:05:47,710
So I'm just going to assume that they're still signed in.
64
00:05:48,100 --> 00:05:53,950
And because of our subscriber here, our subscription is always listening to the OTH, the authors like,
65
00:05:53,950 --> 00:06:00,460
oh, I'm going to send them that user authenticated object every time until they sign out.
66
00:06:01,150 --> 00:06:08,620
So this is a really great feature because this is essentially how we're able to get persistence of our
67
00:06:08,620 --> 00:06:14,050
user sessions without having to set up all this complicated stuff that it would normally require for
68
00:06:14,050 --> 00:06:15,210
us to get this.
69
00:06:15,820 --> 00:06:21,640
But let's look back at the actual user authenticated object that we're getting back from Firebase.
70
00:06:22,120 --> 00:06:26,680
And on this, we just want to ignore a lot of these values because we don't need them, because it's
71
00:06:26,680 --> 00:06:28,000
a Google user object.
72
00:06:28,000 --> 00:06:29,140
It has a lot of stuff.
73
00:06:29,140 --> 00:06:33,880
But the main one we want to look at is display name and email.
74
00:06:34,690 --> 00:06:38,140
So we know this is that user that we just got.
75
00:06:38,740 --> 00:06:47,080
And the great thing about this is that now we have off so easily right out the gate off essentially
76
00:06:47,080 --> 00:06:53,020
allows users to sign in with any other third party service that they might already have.
77
00:06:53,680 --> 00:07:01,120
And by doing this, we've reduced the barrier of entry for allowing users to sign into our application
78
00:07:01,840 --> 00:07:07,270
because they don't have to actually go through an arduous sign-up process or remember their email and
79
00:07:07,270 --> 00:07:08,650
password for our app.
80
00:07:08,830 --> 00:07:11,980
They can just use sign with Google a lot.
81
00:07:11,980 --> 00:07:19,450
Sinon is actually a very tedious and difficult thing to set up from scratch, but by using Firebase,
82
00:07:19,960 --> 00:07:26,830
we got access to it with just a couple configurations and that's why Firebase is such an incredible
83
00:07:26,830 --> 00:07:27,430
platform.
84
00:07:28,150 --> 00:07:34,400
Now, the next thing we have to do is actually understand why this is an open subscription.
85
00:07:34,930 --> 00:07:42,430
So this open subscription, it's an open messaging system between our application and our firebase app.
86
00:07:43,380 --> 00:07:51,660
Whenever any changes occur on Firebase from any source related to this application, Firebase sends
87
00:07:51,660 --> 00:07:57,390
out a message that says that the off status change the user has updated, whether they've signed in
88
00:07:57,390 --> 00:08:02,070
through some service, such as our Google Sinon or our email and password sign up.
89
00:08:02,940 --> 00:08:10,500
Or they've signed out and then they will give us this user and it will call it so we don't actually
90
00:08:10,500 --> 00:08:14,620
have to manually fetch every time we want to check if that state has changed.
91
00:08:14,970 --> 00:08:21,960
This connection is always open as long as our application component is mounted on our job.
92
00:08:22,680 --> 00:08:31,050
But because it's an open subscription, we also have to close subscriptions when this unmount because
93
00:08:31,050 --> 00:08:34,260
we don't want any memory leaks in our JavaScript application.
94
00:08:35,289 --> 00:08:41,919
So how we do that is we would call a new method called unsubscribe.
95
00:08:42,870 --> 00:08:45,660
From off, which by default is equal to none.
96
00:08:45,690 --> 00:08:50,610
So we're setting up property called unsubscribe from off on our class.
97
00:08:51,630 --> 00:08:54,710
And then we're going to say this equals off.
98
00:08:57,090 --> 00:09:03,750
On off state changed, which gives us back a function that when we call closes the script subscription.
99
00:09:04,110 --> 00:09:04,470
Right.
100
00:09:04,710 --> 00:09:09,310
And we want to close it whenever our component on mounts.
101
00:09:09,330 --> 00:09:13,710
So in this new Life-cycle method, a component will unmount.
102
00:09:14,660 --> 00:09:17,300
We want to call this unsubscribe.
103
00:09:18,290 --> 00:09:22,010
From off, and that will close the subscription.
104
00:09:23,340 --> 00:09:33,790
And this is pretty much how we handle our application, being aware of any off changes on Firebase.
105
00:09:34,440 --> 00:09:38,910
I know it's very conceptual and if you understand, that's awesome.
106
00:09:38,910 --> 00:09:41,040
If you don't, that's perfectly fine.
107
00:09:41,040 --> 00:09:43,320
It will make sense the more you do it.
11179
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.