Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,300 --> 00:00:04,650
In this lecture, we're going to log the user in after they've registered.
2
00:00:04,680 --> 00:00:08,790
The registration is taken care of in the register form component.
3
00:00:08,790 --> 00:00:14,460
In the register function, we're using the create user with email and password function to create the
4
00:00:14,460 --> 00:00:15,150
user.
5
00:00:15,150 --> 00:00:20,850
It's also authenticating the user if their account was successfully created, the response from the
6
00:00:20,850 --> 00:00:23,310
function will be the user's credentials.
7
00:00:23,460 --> 00:00:28,140
We're storing the returned value in a variable called user credentials.
8
00:00:28,140 --> 00:00:30,690
We don't need to do anything with the variable.
9
00:00:30,690 --> 00:00:36,660
In the previous lecture, I discussed how we'd need to store the token returned by the authentication
10
00:00:36,660 --> 00:00:37,560
functions.
11
00:00:37,560 --> 00:00:44,760
Luckily, since we're using the SDK that's taken care of for us, Firebase will store the token on our
12
00:00:44,760 --> 00:00:45,600
behalf.
13
00:00:45,840 --> 00:00:50,790
In addition, it'll send the token if it's available to any service that needs it.
14
00:00:50,790 --> 00:00:52,710
This includes Fire Store.
15
00:00:52,710 --> 00:00:57,750
Below this, we're making the request to the database to insert the form data.
16
00:00:58,050 --> 00:01:03,930
We recently changed the rules to restrict writing permissions to authenticated users.
17
00:01:03,930 --> 00:01:08,520
The Firebase SDK will send the token along with this request.
18
00:01:08,520 --> 00:01:11,610
The code will continue to work as it did before.
19
00:01:11,970 --> 00:01:15,630
Let's try testing that out in the Firebase console.
20
00:01:15,630 --> 00:01:18,390
Navigate to the authentication section.
21
00:01:20,750 --> 00:01:23,710
We're going to delete the user we've already registered.
22
00:01:23,720 --> 00:01:28,750
This is so we can start fresh all the way to the right click on the dropdown button.
23
00:01:28,760 --> 00:01:30,890
Delete the user from the app.
24
00:01:33,170 --> 00:01:35,810
Next navigate to the database.
25
00:01:37,950 --> 00:01:40,290
We're not going to delete the collection.
26
00:01:40,290 --> 00:01:42,390
We want to delete the document.
27
00:01:42,420 --> 00:01:46,740
Documents can be deleted on the right column at the top right corner.
28
00:01:46,740 --> 00:01:49,470
Click the dropdown to delete the document.
29
00:01:51,840 --> 00:01:54,230
We're ready to test the registration.
30
00:01:54,240 --> 00:01:55,930
Open the app in the browser.
31
00:01:55,950 --> 00:01:58,020
Try registering a new account.
32
00:02:05,070 --> 00:02:07,690
We can't successfully create an account.
33
00:02:07,710 --> 00:02:13,590
We didn't have to modify the code because Firebase sends the token to the database on our behalf.
34
00:02:13,740 --> 00:02:18,690
This process is the most significant benefit of using the Firebase SDK.
35
00:02:18,720 --> 00:02:21,510
It'll store the token and send it for us.
36
00:02:21,810 --> 00:02:24,630
There is one important point I want to make clear.
37
00:02:24,630 --> 00:02:30,570
Back in the register function, we're registering the user in the authentication service before we insert
38
00:02:30,570 --> 00:02:32,130
them into the database.
39
00:02:34,270 --> 00:02:37,930
We must perform these sequences of actions in this order.
40
00:02:37,960 --> 00:02:43,730
The token will not be available until the user is authenticated with the authentication service.
41
00:02:43,750 --> 00:02:49,450
If we were to swamp the order, Firebase would throw an error because the user does not have permission
42
00:02:49,450 --> 00:02:51,070
to write to the database.
43
00:02:51,250 --> 00:02:57,010
We're going to make one more modification to the register function, even though Firebase is keeping
44
00:02:57,010 --> 00:02:58,520
track of everything for us.
45
00:02:58,540 --> 00:03:04,180
We will store a boolean value in the state to keep track of if the user is logged in.
46
00:03:04,210 --> 00:03:07,000
Let's look at our app in the browser for a moment.
47
00:03:09,240 --> 00:03:13,500
If we were to look at the navigation menu, we have a link for logging in.
48
00:03:13,530 --> 00:03:19,770
However, if the user is already logged in, then we'll want to change this to say log out.
49
00:03:19,800 --> 00:03:25,200
We can toggle the links by keeping track of the user's current authenticated state in the store.
50
00:03:25,230 --> 00:03:28,740
Switch over to your editor in the store directory.
51
00:03:28,770 --> 00:03:32,220
Create a new file called User JS.
52
00:03:34,250 --> 00:03:38,700
For this demonstration, we're going to create a new store for users.
53
00:03:38,720 --> 00:03:41,810
It's completely possible to create a single store.
54
00:03:41,840 --> 00:03:47,810
However, it's common practice to organize the state into separate stores for better management.
55
00:03:47,840 --> 00:03:53,000
In this file, import the define store function from the penny package.
56
00:03:55,120 --> 00:03:59,080
Next export this function from the default namespace.
57
00:04:01,350 --> 00:04:04,440
The ID of the store will be called user.
58
00:04:06,670 --> 00:04:10,750
Passing in an object in this object, define these state function.
59
00:04:12,820 --> 00:04:19,269
Lastly at a state property called user logged in with an initial value of false.
60
00:04:21,390 --> 00:04:27,870
The next step is to toggle this property after the user successfully registers and accounts back in
61
00:04:27,870 --> 00:04:29,590
the register form component.
62
00:04:29,610 --> 00:04:32,130
We're going to update the register function.
63
00:04:34,190 --> 00:04:40,970
First, the store needs to be imported and registered with the components at the top of the script block.
64
00:04:41,000 --> 00:04:43,550
Import the map writable state.
65
00:04:43,550 --> 00:04:45,980
Function from the Linea package.
66
00:04:48,190 --> 00:04:50,890
We're going to update the state of the component.
67
00:04:50,890 --> 00:04:55,370
Therefore we are importing the writable function to allow us to do so.
68
00:04:55,390 --> 00:04:59,470
Next import the user store with the following name.
69
00:04:59,470 --> 00:05:01,480
Use User Store.
70
00:05:03,540 --> 00:05:04,740
We've got everything.
71
00:05:04,740 --> 00:05:10,110
We need time to register the state inside the components exported object.
72
00:05:10,110 --> 00:05:12,030
Add the computed object.
73
00:05:14,070 --> 00:05:20,010
Within this object called the map writable state function with the spread operator.
74
00:05:22,180 --> 00:05:26,680
Pass in the use user store variable as the first argument.
75
00:05:28,840 --> 00:05:35,650
The second argument is an array of state properties to map the components and the user logged in state
76
00:05:35,650 --> 00:05:36,490
property.
77
00:05:38,710 --> 00:05:40,860
It's time to update this property.
78
00:05:40,870 --> 00:05:44,550
The state should be updated after the user has been registered.
79
00:05:44,560 --> 00:05:51,100
I'm going to update this property before updating the alert message set the user logged in property
80
00:05:51,100 --> 00:05:52,000
to true.
81
00:05:56,390 --> 00:06:00,670
As simple as that, we've authenticated the user into our application.
82
00:06:00,680 --> 00:06:04,430
Firebase handles most of the complexities on our side.
83
00:06:04,430 --> 00:06:09,770
We just need to keep track of the user's authentication status with a boolean value.
84
00:06:09,890 --> 00:06:11,600
Let's give this a test.
85
00:06:11,630 --> 00:06:13,430
Open the Firebase console.
86
00:06:13,460 --> 00:06:18,140
Delete any users in the authentication service or users collection.
87
00:06:24,720 --> 00:06:31,230
We want to start fresh one more time, then try filling out the registration form in the app and submit
88
00:06:31,230 --> 00:06:31,620
it.
89
00:06:39,090 --> 00:06:41,680
We will receive a success message.
90
00:06:41,700 --> 00:06:42,380
Great.
91
00:06:42,390 --> 00:06:47,760
We are successfully keeping track of the user's current authenticated status in the state.
8683
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.