1 00:00:00,090 --> 00:00:05,220 In this lecture, we were going to authenticate the user after they have registered for an account, 2 00:00:05,520 --> 00:00:09,630 believe it or not, we won't have to write code to accomplish this task. 3 00:00:09,810 --> 00:00:15,330 Let me show you why registration is written inside the authentication service file. 4 00:00:15,540 --> 00:00:17,400 Let's open it in our editors. 5 00:00:19,840 --> 00:00:25,870 In the create user function, we're running the Create user with email and password function to create 6 00:00:25,870 --> 00:00:26,470 the user. 7 00:00:26,710 --> 00:00:30,730 If you can recall, we read the description of this function together. 8 00:00:31,120 --> 00:00:37,060 The description states Firebase will automatically authenticate the user after creating their account 9 00:00:37,600 --> 00:00:38,950 in the previous lecture. 10 00:00:39,130 --> 00:00:43,720 I discussed how to store the token returned by the authentication functions. 11 00:00:44,170 --> 00:00:51,460 Luckily, since we're using the SDK that's taken care of for us, Firebase will store the token on our 12 00:00:51,460 --> 00:00:52,120 behalf. 13 00:00:52,600 --> 00:00:59,200 In addition, whenever we initiate a request, Firebase will attach the token with our request. 14 00:00:59,530 --> 00:01:01,750 This includes database requests. 15 00:01:02,080 --> 00:01:08,170 Below this function, we're initiating another request to the database to insert the form data. 16 00:01:08,470 --> 00:01:13,810 We recently changed the rules to restrict writing permissions to authenticated users. 17 00:01:14,170 --> 00:01:18,490 The Firebase SDK will send the token along with this request. 18 00:01:18,820 --> 00:01:21,820 The code will continue to work as it did before. 19 00:01:22,420 --> 00:01:26,050 Let's try testing that out in the Firebase console. 20 00:01:26,230 --> 00:01:28,870 Navigate to the authentication section. 21 00:01:31,430 --> 00:01:34,790 We're going to delete the user we've registered within our app. 22 00:01:34,940 --> 00:01:37,980 We will be starting fresh all the way to the right. 23 00:01:38,240 --> 00:01:39,740 Click on the dropdown button. 24 00:01:39,920 --> 00:01:41,720 Delete the user from the app. 25 00:01:44,200 --> 00:01:46,690 Next, navigate to the database. 26 00:01:49,340 --> 00:01:51,330 We're not going to delete the collection. 27 00:01:51,440 --> 00:01:53,180 We want to delete the document. 28 00:01:53,450 --> 00:01:57,680 Documents can be deleted on the right column at the top right corner. 29 00:01:57,770 --> 00:02:00,350 Click the drop down to delete the document. 30 00:02:05,520 --> 00:02:09,509 We're ready to test the registration, open the app in the browser. 31 00:02:09,720 --> 00:02:11,640 Try registering a new accounts. 32 00:02:15,920 --> 00:02:18,380 We can successfully create an account. 33 00:02:18,650 --> 00:02:24,830 We didn't need to modify our code base, Firebase sends a token to the database on our behalf. 34 00:02:25,220 --> 00:02:30,200 This process is the most significant benefit of using the Firebase SDK. 35 00:02:30,620 --> 00:02:33,710 It'll store the token and send it with requests. 36 00:02:34,310 --> 00:02:40,400 There is one crucial point I want to make clear back in the service, we're registering the user in 37 00:02:40,400 --> 00:02:44,360 the authentication service before we insert them into the database. 38 00:02:46,830 --> 00:02:50,280 We must perform these sequences of actions in this order. 39 00:02:50,610 --> 00:02:56,400 The token will not be available until the user is authenticated with the authentication service. 40 00:02:56,730 --> 00:03:02,460 If we were to swap the order, Firebase would throw an error because the user does not have permission 41 00:03:02,460 --> 00:03:04,050 to write to the database. 42 00:03:04,560 --> 00:03:10,720 As I stated from the beginning, managing the authentication of the user is automated with Firebase 43 00:03:10,720 --> 00:03:11,670 is SDK. 44 00:03:12,030 --> 00:03:17,700 In the next lecture, we're going to learn how to check the user's authentication status throughout 45 00:03:17,700 --> 00:03:18,360 our app.