Fireship · @fireship
Custom Usernames with Firebase Authentication
Uploaded June 26, 2017 · Length 5:40
Summary
Create custom usernames for Firebase users and validate them asynchronously using Angular 4.
- firebase authentication
- custom usernames
- angular 4
- username validation
- firebase database rules
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
in this episode we're going to create
- 0:02
custom usernames for Firebase users then
- 0:05
validate the usernames asynchronously
- 0:07
using angular
- 0:08
4 users will first authenticate with
- 0:11
ooth then they'll be directed to a
- 0:13
username form and after each key press
- 0:16
the username will be updated based on
- 0:19
whether or not it's available in the
- 0:21
database the first step is to model the
- 0:24
data first we have a user's collection
- 0:27
that saves just basic information about
- 0:29
the user that we want to display
- 0:30
throughout the app then we have a
- 0:32
separate collection of just usernames
- 0:35
where the username itself is the key and
- 0:37
the value is the associated user ID this
- 0:40
allows us to make checks for username
- 0:42
availability much more quickly than we
- 0:44
could if we query the entire list of
- 0:46
[Music]
- 0:48
users now we can start building the off
- 0:51
service we're going to import the
- 0:53
angular Fire 2 package both the database
- 0:55
and the off
- 0:57
module we also import the RX xjs switch
- 1:00
map operator which we'll use to avoid
- 1:03
nested subscriptions when we get the
- 1:04
user
- 1:07
data I'm also creating a dedicated user
- 1:10
class to hold the user data in this case
- 1:13
it's just going to take a username and a
- 1:15
user ID and as the Constructor it'll
- 1:17
take the Firebase off
- 1:24
object in the service Constructor we
- 1:26
first wait for the O object to emit from
- 1:29
Firebase
- 1:30
and then we use that data to query the
- 1:33
database for the actual
- 1:43
username the first function is going to
- 1:46
allow the user to authenticate with
- 1:47
their Google account this codes directly
- 1:50
from the angular Fire 2
- 1:52
package to manage the username process
- 1:55
we start by creating a getter to see if
- 1:57
a username is defined or not


