Fireship · @fireship

How to Connect Firebase Users to their Data - 3 Methods

Uploaded July 2, 2017 · Length 5:53

How to Connect Firebase Users to their Data - 3 Methods

Summary

Fireship shows three methods to connect Firebase users to their data, including nesting under user ID, saving user ID as item attribute, and multiple user association.

  • firebase
  • user data association
  • firebase nosql database
  • angular 4
  • firebase security 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 I'm going to talk about

  2. 0:02

    ways to associate Firebase users with

  3. 0:04

    data in the real-time database I'll go

  4. 0:07

    over three different methods and when to

  5. 0:09

    use them and also talk about ways to

  6. 0:12

    keep them secure on the back end but

  7. 0:14

    before we get into that it's important

  8. 0:15

    to go over a couple nosql database

  9. 0:18

    design best practices if you come from a

  10. 0:21

    SQL background you're used to designing

  11. 0:23

    a database that is completely centered

  12. 0:25

    around relationships in no SQL we care

  13. 0:27

    less about relationships and more about

  14. 0:30

    about the efficiency of the actual query

  15. 0:31

    that we're making consider a database

  16. 0:34

    design that has everything nested under

  17. 0:36

    the user ID if you wanted to Loop over

  18. 0:38

    just the users and display their

  19. 0:40

    username you'd also have to load all the

  20. 0:42

    nested data further down the tree which

  21. 0:44

    would become very inefficient if you

  22. 0:46

    have a lot of Records a better way to

  23. 0:48

    handle this is to keep the data shallow

  24. 0:50

    so you only query for the actual data

  25. 0:52

    you need now we can start building some

  26. 0:55

    user relationships with Firebase and

  27. 0:57

    angular 4 the first method work works by

  28. 1:00

    nesting a user's items under their own

  29. 1:03

    user ID this is ideal when you're

  30. 1:05

    showing data that's organized by user

  31. 1:07

    for example a Twitter

  32. 1:10

    feed here we have an item service and

  33. 1:13

    we're going to import both the angular

  34. 1:15

    fire database and the angular fire off

  35. 1:18

    module then we create an item class

  36. 1:20

    which represents our data at this point

  37. 1:22

    with just one body string

  38. 1:25

    attribute this class will be returned to

  39. 1:27

    us as an angular fire list Observer

  40. 1:31

    when we inject the service we want to

  41. 1:33

    subscribe to the angular fire off State

  42. 1:36

    and we'll set the user ID return from

  43. 1:38

    that off state to the user ID on the

  44. 1:41

    service this allows us to make queries

  45. 1:43

    based on the currently logged in users

  46. 1:46

    ID now that we have the user ID we can

  47. 1:50

    hit the database for the list that is

  48. 1:52

    nested under that user ID if it's

  49. 1:54

    undefined we just return null it's

  50. 1:56

    defined we interpolate it into the path

  51. 1:59

    to the