Fireship · @fireship
How to Connect Firebase Users to their Data - 3 Methods
Uploaded July 2, 2017 · Length 5:53
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.
- 0:00
in this episode I'm going to talk about
- 0:02
ways to associate Firebase users with
- 0:04
data in the real-time database I'll go
- 0:07
over three different methods and when to
- 0:09
use them and also talk about ways to
- 0:12
keep them secure on the back end but
- 0:14
before we get into that it's important
- 0:15
to go over a couple nosql database
- 0:18
design best practices if you come from a
- 0:21
SQL background you're used to designing
- 0:23
a database that is completely centered
- 0:25
around relationships in no SQL we care
- 0:27
less about relationships and more about
- 0:30
about the efficiency of the actual query
- 0:31
that we're making consider a database
- 0:34
design that has everything nested under
- 0:36
the user ID if you wanted to Loop over
- 0:38
just the users and display their
- 0:40
username you'd also have to load all the
- 0:42
nested data further down the tree which
- 0:44
would become very inefficient if you
- 0:46
have a lot of Records a better way to
- 0:48
handle this is to keep the data shallow
- 0:50
so you only query for the actual data
- 0:52
you need now we can start building some
- 0:55
user relationships with Firebase and
- 0:57
angular 4 the first method work works by
- 1:00
nesting a user's items under their own
- 1:03
user ID this is ideal when you're
- 1:05
showing data that's organized by user
- 1:07
for example a Twitter
- 1:10
feed here we have an item service and
- 1:13
we're going to import both the angular
- 1:15
fire database and the angular fire off
- 1:18
module then we create an item class
- 1:20
which represents our data at this point
- 1:22
with just one body string
- 1:25
attribute this class will be returned to
- 1:27
us as an angular fire list Observer
- 1:31
when we inject the service we want to
- 1:33
subscribe to the angular fire off State
- 1:36
and we'll set the user ID return from
- 1:38
that off state to the user ID on the
- 1:41
service this allows us to make queries
- 1:43
based on the currently logged in users
- 1:46
ID now that we have the user ID we can
- 1:50
hit the database for the list that is
- 1:52
nested under that user ID if it's
- 1:54
undefined we just return null it's
- 1:56
defined we interpolate it into the path
- 1:59
to the


