Fireship · @fireship

Custom Usernames with Firebase Authentication

Uploaded June 26, 2017 · Length 5:40

Custom Usernames with Firebase Authentication

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.

  1. 0:00

    in this episode we're going to create

  2. 0:02

    custom usernames for Firebase users then

  3. 0:05

    validate the usernames asynchronously

  4. 0:07

    using angular

  5. 0:08

    4 users will first authenticate with

  6. 0:11

    ooth then they'll be directed to a

  7. 0:13

    username form and after each key press

  8. 0:16

    the username will be updated based on

  9. 0:19

    whether or not it's available in the

  10. 0:21

    database the first step is to model the

  11. 0:24

    data first we have a user's collection

  12. 0:27

    that saves just basic information about

  13. 0:29

    the user that we want to display

  14. 0:30

    throughout the app then we have a

  15. 0:32

    separate collection of just usernames

  16. 0:35

    where the username itself is the key and

  17. 0:37

    the value is the associated user ID this

  18. 0:40

    allows us to make checks for username

  19. 0:42

    availability much more quickly than we

  20. 0:44

    could if we query the entire list of

  21. 0:46

    [Music]

  22. 0:48

    users now we can start building the off

  23. 0:51

    service we're going to import the

  24. 0:53

    angular Fire 2 package both the database

  25. 0:55

    and the off

  26. 0:57

    module we also import the RX xjs switch

  27. 1:00

    map operator which we'll use to avoid

  28. 1:03

    nested subscriptions when we get the

  29. 1:04

    user

  30. 1:07

    data I'm also creating a dedicated user

  31. 1:10

    class to hold the user data in this case

  32. 1:13

    it's just going to take a username and a

  33. 1:15

    user ID and as the Constructor it'll

  34. 1:17

    take the Firebase off

  35. 1:24

    object in the service Constructor we

  36. 1:26

    first wait for the O object to emit from

  37. 1:29

    Firebase

  38. 1:30

    and then we use that data to query the

  39. 1:33

    database for the actual

  40. 1:43

    username the first function is going to

  41. 1:46

    allow the user to authenticate with

  42. 1:47

    their Google account this codes directly

  43. 1:50

    from the angular Fire 2

  44. 1:52

    package to manage the username process

  45. 1:55

    we start by creating a getter to see if

  46. 1:57

    a username is defined or not