In this lecture, we were going to authenticate the user after they have registered for an account,
believe it or not, we won't have to write code to accomplish this task.
Let me show you why registration is written inside the authentication service file.
Let's open it in our editors.
In the create user function, we're running the Create user with email and password function to create
the user.
If you can recall, we read the description of this function together.
The description states Firebase will automatically authenticate the user after creating their account
in the previous lecture.
I discussed how to store the token returned by the authentication functions.
Luckily, since we're using the SDK that's taken care of for us, Firebase will store the token on our
behalf.
In addition, whenever we initiate a request, Firebase will attach the token with our request.
This includes database requests.
Below this function, we're initiating another request to the database to insert the form data.
We recently changed the rules to restrict writing permissions to authenticated users.
The Firebase SDK will send the token along with this request.
The code will continue to work as it did before.
Let's try testing that out in the Firebase console.
Navigate to the authentication section.
We're going to delete the user we've registered within our app.
We will be starting fresh all the way to the right.
Click on the dropdown button.
Delete the user from the app.
Next, navigate to the database.
We're not going to delete the collection.
We want to delete the document.
Documents can be deleted on the right column at the top right corner.
Click the drop down to delete the document.
We're ready to test the registration, open the app in the browser.
Try registering a new accounts.
We can successfully create an account.
We didn't need to modify our code base, Firebase sends a token to the database on our behalf.
This process is the most significant benefit of using the Firebase SDK.
It'll store the token and send it with requests.
There is one crucial point I want to make clear back in the service, we're registering the user in
the authentication service before we insert them into the database.
We must perform these sequences of actions in this order.
The token will not be available until the user is authenticated with the authentication service.
If we were to swap the order, Firebase would throw an error because the user does not have permission
to write to the database.
As I stated from the beginning, managing the authentication of the user is automated with Firebase
is SDK.
In the next lecture, we're going to learn how to check the user's authentication status throughout
our app.