Hello,
In this article I will show you how to authenticate to Firebase and secure the databases with some simple security rules. I’m using this setup for my NucuCar project.
Authentication
The first step is to enable the Email/Password sign in method, by going to the Authentication and clicking the Sign-In Method tab:
Next, we can add a manual user by clicking Users tab and Add user.
Now, to login with our newly created user, we make a POST request with a json body to the following endpoint:
|
|
Note that you need to fill in the WEB_API_KEY parameter as well! You can obtain it by clicking the settings gear next to Project Overview. The General tab should open and it should list the Web API Key.
If the login is successful you will get a response similar to the following:
|
|
In order to make authenticated requests you’ll have to provide the following header to all requests:
Authentication: Bearer ${idToken}
Keep in mind that the idToken expires in 3600 seconds and you’ll need to exchange the refresh token for a new idToken before or after it expires:
|
|
Security Rules
To secure my database I’ve used the following security rules. The don’t allow deletion and only allow creating, updating and reading documents. Deleting them is forbidden.
|
|
Thanks for reading!