Fully documented lesson at https://angularfirebase.com/lessons/managing-firebase-user-relationships-to-database-records/

Learn how to maintain user/data relationships with Firebase users and data from the Realtime database. I provide three different methods for commonly encountered scenarios.
2024-02-04T08:28:52.920Z
nova-2
2-general-nova
2024-01-09.29447
30089e05-99d1-4376-b32e-c263170674af
d36f9e4d-d00c-4be8-98f7-e1285ac535de
f49935d40997ad068ddf353c717514c26453a10255d206c148ce201fd6d01987
67875a7f-c9c4-48a0-aa55-5bdb8a91c34a
deprecated
In this episode, I'm going to talk about ways to associate Firebase users with data in the real time database.
I'll go over 3 different methods and when to use them and also talk about ways to keep them secure on the back end.
But before we get into that, it's important to go over a couple NoSQL database design best practices.
If you come from a SQL background, you're used to designing a database that is completely centered around relationships.
In NoSQL, we care less about relationships and more about the efficiency of the actual query that we're making.
Consider a database design that has everything nested under the user ID.
If you wanted to loop over just the users and display their username, You'd also have to load all the nested data further down the tree which would become very inefficient if you have a lot of records.
A better way to handle this is to keep the data shallow so you only query for the actual data you need.
Now we can start building some user relationships with Firebase and Angular for the first method works by nesting a user's items under their own user ID.
This is ideal when you're showing data that's organized by user, for example a Twitter feed.
Here we have an item service and we're going to import both the AngularFire database in the AngularFire auth module.
Then we create an item class which represents our data at this point with just one body string attribute.
This class will be returned to us as an AngularFire list observable.
When we inject the service, we want to subscribe to the AngularFire auth state and we'll set the user ID a return from that auth state to the user ID on the service.
This allows us to make queries based on the currently logged in user's ID.
Now that we have the user ID, we can hit the database for the list that is nested under that user ID.
If it's undefined, we just return null.
If it's defined, we interpolate it into the path to the database.
If we wanna create a new item, it's simple as pushing another item object onto this list observable.
Back in the app, we can see that when we create a new item.
It's nested under that user's user ID as expected.
If we log out and log in with a different user, we shouldn't see any items in the feed, which we don't.
And if we create a new item with this user, their data is nested under their user ID, essentially separating the data by user.
This works great on the front end, but we also need to secure the back end data with a Firebase database rule.
In this example, we match the user ID that the data is nested under with the actual auth ID in Firebase.
If it's a match, then we allow the right operation to take place.
A second way to associate users with items is to save the user ID as an attribute on the item itself.
This approach is useful when you need the association but you're not querying by the user.
For example, comments on a blog post or something similar.
We can make this work by modifying 3 lines of code.
First, we add a user ID to the item class.
Then we set that user ID on the item when it's created.
Then we remove the user ID from the database path we're no longer nesting under the user ID.
Back in the app, we create a new item and we can see that user ID is on the item in itself.
And if we create another item with a different user, we should have a different user ID on that item, but still both items are coming up in the feed now.
In Firebase, we can see the user ID attribute is on each item.
To secure the back end, we see if the existing data in the database has a matching user ID.
And we also allow the right to occur if it's null because as that means the user's creating a new item.
The 3rd scenario is when you have items that can be associated with multiple Users.
In this case, we create a collection of members that are nested under the item ID and the key for each member is that user's user ID.
Back in the service, we'll create a new function to join an item, passing it the item's key as an argument.
The data will be the user ID for the key set to true.
We return the current members as an object observable, then update that observable with the new data.
To leave an item, we follow the same basic approach.
We query for an object observable with the current user ID.
And if it exists, we remove it.
Back in the app, we create a new item that defaults to no members, and then we can click the join button, which will add that user ID to the list of members.
When we log in with a different user, we should see that they can also add their or user ID to this item.
Going into Firebase, we can see that the member's collection on this item It's updated with the key after each button is clicked.
That's it for this episode.
If you found the video helpful, please like and subscribe.
And if you wanna support the channel, consider becoming a pro subscriber at angularfirebase.com.
For just a few bucks a month, you'll get access to exclusive content as well as free 1 on 1 project consulting.
Thanks for watching, and I'll see you next time.

Speaker 0: In this episode, I'm going to talk about ways to associate Firebase users with data in the real time database. I'll go over 3 different methods and when to use them and also talk about ways to keep them secure on the back end. But before we get into that, it's important to go over a couple NoSQL database design best practices. If you come from a SQL background, you're used to designing a database that is completely centered around relationships. In NoSQL, we care less about relationships and more about the efficiency of the actual query that we're making.

Consider a database design that has everything nested under the user ID. If you wanted to loop over just the users and display their username, You'd also have to load all the nested data further down the tree which would become very inefficient if you have a lot of records. A better way to handle this is to keep the data shallow so you only query for the actual data you need. Now we can start building some user relationships with Firebase and Angular for the first method works by nesting a user's items under their own user ID. This is ideal when you're showing data that's organized by user, for example a Twitter feed.

Here we have an item service and we're going to import both the AngularFire database in the AngularFire auth module. Then we create an item class which represents our data at this point with just one body string attribute. This class will be returned to us as an AngularFire list observable. When we inject the service, we want to subscribe to the AngularFire auth state and we'll set the user ID a return from that auth state to the user ID on the service. This allows us to make queries based on the currently logged in user's ID.

Now that we have the user ID, we can hit the database for the list that is nested under that user ID. If it's undefined, we just return null. If it's defined, we interpolate it into the path to the database. If we wanna create a new item, it's simple as pushing another item object onto this list observable. Back in the app, we can see that when we create a new item.

It's nested under that user's user ID as expected. If we log out and log in with a different user, we shouldn't see any items in the feed, which we don't. And if we create a new item with this user, their data is nested under their user ID, essentially separating the data by user. This works great on the front end, but we also need to secure the back end data with a Firebase database rule. In this example, we match the user ID that the data is nested under with the actual auth ID in Firebase.

If it's a match, then we allow the right operation to take place. A second way to associate users with items is to save the user ID as an attribute on the item itself. This approach is useful when you need the association but you're not querying by the user. For example, comments on a blog post or something similar. We can make this work by modifying 3 lines of code.

First, we add a user ID to the item class. Then we set that user ID on the item when it's created. Then we remove the user ID from the database path we're no longer nesting under the user ID. Back in the app, we create a new item and we can see that user ID is on the item in itself. And if we create another item with a different user, we should have a different user ID on that item, but still both items are coming up in the feed now.

In Firebase, we can see the user ID attribute is on each item. To secure the back end, we see if the existing data in the database has a matching user ID. And we also allow the right to occur if it's null because as that means the user's creating a new item. The 3rd scenario is when you have items that can be associated with multiple Users. In this case, we create a collection of members that are nested under the item ID and the key for each member is that user's user ID.

Back in the service, we'll create a new function to join an item, passing it the item's key as an argument. The data will be the user ID for the key set to true. We return the current members as an object observable, then update that observable with the new data. To leave an item, we follow the same basic approach. We query for an object observable with the current user ID.

And if it exists, we remove it. Back in the app, we create a new item that defaults to no members, and then we can click the join button, which will add that user ID to the list of members. When we log in with a different user, we should see that they can also add their or user ID to this item. Going into Firebase, we can see that the member's collection on this item It's updated with the key after each button is clicked. That's it for this episode.

If you found the video helpful, please like and subscribe. And if you wanna support the channel, consider becoming a pro subscriber at angularfirebase.com. For just a few bucks a month, you'll get access to exclusive content as well as free 1 on 1 project consulting. Thanks for watching, and I'll see you next time.
In this episode, I'm going to talk about ways to associate Firebase users with data in the real time database. I'll go over 3 different methods and when to use them and also talk about ways to keep them secure on the back end. But before we get into that, it's important to go over a couple NoSQL database design best practices. If you come from a SQL background, you're used to designing a database that is completely centered around relationships. In NoSQL, we care less about relationships and more about the efficiency of the actual query that we're making. Consider a database design that has everything nested under the user ID. If you wanted to loop over just the users and display their username, You'd also have to load all the nested data further down the tree which would become very inefficient if you have a lot of records. A better way to handle this is to keep the data shallow so you only query for the actual data you need. Now we can start building some user relationships with Firebase and Angular for the first method works by nesting a user's items under their own user ID. This is ideal when you're showing data that's organized by user, for example a Twitter feed. Here we have an item service and we're going to import both the AngularFire database in the AngularFire auth module. Then we create an item class which represents our data at this point with just one body string attribute.
databases
data management
data
This class will be returned to us as an AngularFire list observable. When we inject the service, we want to subscribe to the AngularFire auth state and we'll set the user ID a return from that auth state to the user ID on the service.
This allows us to make queries based on the currently logged in user's ID. Now that we have the user ID, we can hit the database for the list that is nested under that user ID. If it's undefined, we just return null. If it's defined, we interpolate it into the path to the database. If we wanna create a new item, it's simple as pushing another item object onto this list observable. Back in the app, we can see that when we create a new item.
databases
It's nested under that user's user ID as expected. If we log out and log in with a different user, we shouldn't see any items in the feed, which we don't. And if we create a new item with this user, their data is nested under their user ID, essentially separating the data by user. This works great on the front end, but we also need to secure the back end data with a Firebase database rule. In this example, we match the user ID that the data is nested under with the actual auth ID in Firebase. If it's a match, then we allow the right operation to take place. A second way to associate users with items is to save the user ID as an attribute on the item itself. This approach is useful when you need the association but you're not querying by the user.
For example, comments on a blog post or something similar. We can make this work by modifying 3 lines of code. First, we add a user ID to the item class. Then we set that user ID on the item when it's created. Then we remove the user ID from the database path we're no longer nesting under the user ID.
blogging
Back in the app, we create a new item and we can see that user ID is on the item in itself. And if we create another item with a different user, we should have a different user ID on that item, but still both items are coming up in the feed now. In Firebase, we can see the user ID attribute is on each item. To secure the back end, we see if the existing data in the database has a matching user ID. And we also allow the right to occur if it's null because as that means the user's creating a new item. The 3rd scenario is when you have items that can be associated with multiple Users. In this case, we create a collection of members that are nested under the item ID and the key for each member is that user's user ID.
Back in the service, we'll create a new function to join an item, passing it the item's key as an argument. The data will be the user ID for the key set to true. We return the current members as an object observable, then update that observable with the new data.
data
To leave an item, we follow the same basic approach. We query for an object observable with the current user ID. And if it exists, we remove it. Back in the app, we create a new item that defaults to no members, and then we can click the join button, which will add that user ID to the list of members. When we log in with a different user, we should see that they can also add their or user ID to this item. Going into Firebase, we can see that the member's collection on this item It's updated with the key after each button is clicked.
That's it for this episode. If you found the video helpful, please like and subscribe. And if you wanna support the channel, consider becoming a pro subscriber at angularfirebase.com. For just a few bucks a month, you'll get access to exclusive content as well as free 1 on 1 project consulting.
youtube
streaming
podcasts
Thanks for watching, and I'll see you next time.
In this episode, I'm going to talk about ways to associate Firebase users with data in the real time database. I'll go over 3 different methods and when to use them and also talk about ways to keep them secure on the back end. But before we get into that, it's important to go over a couple NoSQL database design best practices. If you come from a SQL background, you're used to designing a database that is completely centered around relationships. In NoSQL, we care less about relationships and more about the efficiency of the actual query that we're making. Consider a database design that has everything nested under the user ID. If you wanted to loop over just the users and display their username, You'd also have to load all the nested data further down the tree which would become very inefficient if you have a lot of records. A better way to handle this is to keep the data shallow so you only query for the actual data you need. Now we can start building some user relationships with Firebase and Angular for the first method works by nesting a user's items under their own user ID. This is ideal when you're showing data that's organized by user, for example a Twitter feed. Here we have an item service and we're going to import both the AngularFire database in the AngularFire auth module. Then we create an item class which represents our data at this point with just one body string attribute. This class will be returned to us as an AngularFire list observable. When we inject the service, we want to subscribe to the AngularFire auth state and we'll set the user ID a return from that auth state to the user ID on the service. This allows us to make queries based on the currently logged in user's ID. Now that we have the user ID, we can hit the database for the list that is nested under that user ID. If it's undefined, we just return null. If it's defined, we interpolate it into the path to the database. If we wanna create a new item, it's simple as pushing another item object onto this list observable. Back in the app, we can see that when we create a new item. It's nested under that user's user ID as expected. If we log out and log in with a different user, we shouldn't see any items in the feed, which we don't. And if we create a new item with this user, their data is nested under their user ID, essentially separating the data by user. This works great on the front end, but we also need to secure the back end data with a Firebase database rule. In this example, we match the user ID that the data is nested under with the actual auth ID in Firebase. If it's a match, then we allow the right operation to take place. A second way to associate users with items is to save the user ID as an attribute on the item itself. This approach is useful when you need the association but you're not querying by the user. For example, comments on a blog post or something similar. We can make this work by modifying 3 lines of code. First, we add a user ID to the item class. Then we set that user ID on the item when it's created. Then we remove the user ID from the database path we're no longer nesting under the user ID. Back in the app, we create a new item and we can see that user ID is on the item in itself. And if we create another item with a different user, we should have a different user ID on that item, but still both items are coming up in the feed now. In Firebase, we can see the user ID attribute is on each item. To secure the back end, we see if the existing data in the database has a matching user ID. And we also allow the right to occur if it's null because as that means the user's creating a new item. The 3rd scenario is when you have items that can be associated with multiple Users. In this case, we create a collection of members that are nested under the item ID and the key for each member is that user's user ID. Back in the service, we'll create a new function to join an item, passing it the item's key as an argument. The data will be the user ID for the key set to true. We return the current members as an object observable, then update that observable with the new data. To leave an item, we follow the same basic approach. We query for an object observable with the current user ID. And if it exists, we remove it. Back in the app, we create a new item that defaults to no members, and then we can click the join button, which will add that user ID to the list of members. When we log in with a different user, we should see that they can also add their or user ID to this item. Going into Firebase, we can see that the member's collection on this item It's updated with the key after each button is clicked. That's it for this episode. If you found the video helpful, please like and subscribe. And if you wanna support the channel, consider becoming a pro subscriber at angularfirebase.com. For just a few bucks a month, you'll get access to exclusive content as well as free 1 on 1 project consulting. Thanks for watching, and I'll see you next time.
In
in
this
this
episode,
episode
I'm
i'm
going
going
to
to
talk
talk
about
about
ways
ways
to
to
associate
associate
Firebase
firebase
users
users
with
with
data
data
in
in
the
the
real
real
time
time
database.
database
I'll
i'll
go
go
over
over
3
3
different
different
methods
methods
and
and
when
when
to
to
use
use
them
them
and
and
also
also
talk
talk
about
about
ways
ways
to
to
keep
keep
them
them
secure
secure
on
on
the
the
back
back
end.
end
But
but
before
before
we
we
get
get
into
into
that,
that
it's
it's
important
important
to
to
go
go
over
over
a
a
couple
couple
NoSQL
nosql
database
database
design
design
best
best
practices.
practices
If
if
you
you
come
come
from
from
a
a
SQL
sql
background,
background
you're
you're
used
used
to
to
designing
designing
a
a
database
database
that
that
is
is
completely
completely
centered
centered
around
around
relationships.
relationships
In
in
NoSQL,
nosql
we
we
care
care
less
less
about
about
relationships
relationships
and
and
more
more
about
about
the
the
efficiency
efficiency
of
of
the
the
actual
actual
query
query
that
that
we're
we're
making.
making
Consider
consider
a
a
database
database
design
design
that
that
has
has
everything
everything
nested
nested
under
under
the
the
user
user
ID.
id
If
if
you
you
wanted
wanted
to
to
loop
loop
over
over
just
just
the
the
users
users
and
and
display
display
their
their
username,
username
You'd
you'd
also
also
have
have
to
to
load
load
all
all
the
the
nested
nested
data
data
further
further
down
down
the
the
tree
tree
which
which
would
would
become
become
very
very
inefficient
inefficient
if
if
you
you
have
have
a
a
lot
lot
of
of
records.
records
A
a
better
better
way
way
to
to
handle
handle
this
this
is
is
to
to
keep
keep
the
the
data
data
shallow
shallow
so
so
you
you
only
only
query
query
for
for
the
the
actual
actual
data
data
you
you
need.
need
Now
now
we
we
can
can
start
start
building
building
some
some
user
user
relationships
relationships
with
with
Firebase
firebase
and
and
Angular
angular
for
for
the
the
first
first
method
method
works
works
by
by
nesting
nesting
a
a
user's
user's
items
items
under
under
their
their
own
own
user
user
ID.
id
This
this
is
is
ideal
ideal
when
when
you're
you're
showing
showing
data
data
that's
that's
organized
organized
by
by
user,
user
for
for
example
example
a
a
Twitter
twitter
feed.
feed
Here
here
we
we
have
have
an
an
item
item
service
service
and
and
we're
we're
going
going
to
to
import
import
both
both
the
the
AngularFire
angularfire
database
database
in
in
the
the
AngularFire
angularfire
auth
auth
module.
module
Then
then
we
we
create
create
an
an
item
item
class
class
which
which
represents
represents
our
our
data
data
at
at
this
this
point
point
with
with
just
just
one
one
body
body
string
string
attribute.
attribute
This
this
class
class
will
will
be
be
returned
returned
to
to
us
us
as
as
an
an
AngularFire
angularfire
list
list
observable.
observable
When
when
we
we
inject
inject
the
the
service,
service
we
we
want
want
to
to
subscribe
subscribe
to
to
the
the
AngularFire
angularfire
auth
auth
state
state
and
and
we'll
we'll
set
set
the
the
user
user
ID
id
a
a
return
return
from
from
that
that
auth
auth
state
state
to
to
the
the
user
user
ID
id
on
on
the
the
service.
service
This
this
allows
allows
us
us
to
to
make
make
queries
queries
based
based
on
on
the
the
currently
currently
logged
logged
in
in
user's
user's
ID.
id
Now
now
that
that
we
we
have
have
the
the
user
user
ID,
id
we
we
can
can
hit
hit
the
the
database
database
for
for
the
the
list
list
that
that
is
is
nested
nested
under
under
that
that
user
user
ID.
id
If
if
it's
it's
undefined,
undefined
we
we
just
just
return
return
null.
null
If
if
it's
it's
defined,
defined
we
we
interpolate
interpolate
it
it
into
into
the
the
path
path
to
to
the
the
database.
database
If
if
we
we
wanna
wanna
create
create
a
a
new
new
item,
item
it's
it's
simple
simple
as
as
pushing
pushing
another
another
item
item
object
object
onto
onto
this
this
list
list
observable.
observable
Back
back
in
in
the
the
app,
app
we
we
can
can
see
see
that
that
when
when
we
we
create
create
a
a
new
new
item.
item
It's
it's
nested
nested
under
under
that
that
user's
user's
user
user
ID
id
as
as
expected.
expected
If
if
we
we
log
log
out
out
and
and
log
log
in
in
with
with
a
a
different
different
user,
user
we
we
shouldn't
shouldn't
see
see
any
any
items
items
in
in
the
the
feed,
feed
which
which
we
we
don't.
don't
And
and
if
if
we
we
create
create
a
a
new
new
item
item
with
with
this
this
user,
user
their
their
data
data
is
is
nested
nested
under
under
their
their
user
user
ID,
id
essentially
essentially
separating
separating
the
the
data
data
by
by
user.
user
This
this
works
works
great
great
on
on
the
the
front
front
end,
end
but
but
we
we
also
also
need
need
to
to
secure
secure
the
the
back
back
end
end
data
data
with
with
a
a
Firebase
firebase
database
database
rule.
rule
In
in
this
this
example,
example
we
we
match
match
the
the
user
user
ID
id
that
that
the
the
data
data
is
is
nested
nested
under
under
with
with
the
the
actual
actual
auth
auth
ID
id
in
in
Firebase.
firebase
If
if
it's
it's
a
a
match,
match
then
then
we
we
allow
allow
the
the
right
right
operation
operation
to
to
take
take
place.
place
A
a
second
second
way
way
to
to
associate
associate
users
users
with
with
items
items
is
is
to
to
save
save
the
the
user
user
ID
id
as
as
an
an
attribute
attribute
on
on
the
the
item
item
itself.
itself
This
this
approach
approach
is
is
useful
useful
when
when
you
you
need
need
the
the
association
association
but
but
you're
you're
not
not
querying
querying
by
by
the
the
user.
user
For
for
example,
example
comments
comments
on
on
a
a
blog
blog
post
post
or
or
something
something
similar.
similar
We
we
can
can
make
make
this
this
work
work
by
by
modifying
modifying
3
3
lines
lines
of
of
code.
code
First,
first
we
we
add
add
a
a
user
user
ID
id
to
to
the
the
item
item
class.
class
Then
then
we
we
set
set
that
that
user
user
ID
id
on
on
the
the
item
item
when
when
it's
it's
created.
created
Then
then
we
we
remove
remove
the
the
user
user
ID
id
from
from
the
the
database
database
path
path
we're
we're
no
no
longer
longer
nesting
nesting
under
under
the
the
user
user
ID.
id
Back
back
in
in
the
the
app,
app
we
we
create
create
a
a
new
new
item
item
and
and
we
we
can
can
see
see
that
that
user
user
ID
id
is
is
on
on
the
the
item
item
in
in
itself.
itself
And
and
if
if
we
we
create
create
another
another
item
item
with
with
a
a
different
different
user,
user
we
we
should
should
have
have
a
a
different
different
user
user
ID
id
on
on
that
that
item,
item
but
but
still
still
both
both
items
items
are
are
coming
coming
up
up
in
in
the
the
feed
feed
now.
now
In
in
Firebase,
firebase
we
we
can
can
see
see
the
the
user
user
ID
id
attribute
attribute
is
is
on
on
each
each
item.
item
To
to
secure
secure
the
the
back
back
end,
end
we
we
see
see
if
if
the
the
existing
existing
data
data
in
in
the
the
database
database
has
has
a
a
matching
matching
user
user
ID.
id
And
and
we
we
also
also
allow
allow
the
the
right
right
to
to
occur
occur
if
if
it's
it's
null
null
because
because
as
as
that
that
means
means
the
the
user's
user's
creating
creating
a
a
new
new
item.
item
The
the
3rd
3rd
scenario
scenario
is
is
when
when
you
you
have
have
items
items
that
that
can
can
be
be
associated
associated
with
with
multiple
multiple
Users.
users
In
in
this
this
case,
case
we
we
create
create
a
a
collection
collection
of
of
members
members
that
that
are
are
nested
nested
under
under
the
the
item
item
ID
id
and
and
the
the
key
key
for
for
each
each
member
member
is
is
that
that
user's
user's
user
user
ID.
id
Back
back
in
in
the
the
service,
service
we'll
we'll
create
create
a
a
new
new
function
function
to
to
join
join
an
an
item,
item
passing
passing
it
it
the
the
item's
item's
key
key
as
as
an
an
argument.
argument
The
the
data
data
will
will
be
be
the
the
user
user
ID
id
for
for
the
the
key
key
set
set
to
to
true.
true
We
we
return
return
the
the
current
current
members
members
as
as
an
an
object
object
observable,
observable
then
then
update
update
that
that
observable
observable
with
with
the
the
new
new
data.
data
To
to
leave
leave
an
an
item,
item
we
we
follow
follow
the
the
same
same
basic
basic
approach.
approach
We
we
query
query
for
for
an
an
object
object
observable
observable
with
with
the
the
current
current
user
user
ID.
id
And
and
if
if
it
it
exists,
exists
we
we
remove
remove
it.
it
Back
back
in
in
the
the
app,
app
we
we
create
create
a
a
new
new
item
item
that
that
defaults
defaults
to
to
no
no
members,
members
and
and
then
then
we
we
can
can
click
click
the
the
join
join
button,
button
which
which
will
will
add
add
that
that
user
user
ID
id
to
to
the
the
list
list
of
of
members.
members
When
when
we
we
log
log
in
in
with
with
a
a
different
different
user,
user
we
we
should
should
see
see
that
that
they
they
can
can
also
also
add
add
their
their
or
or
user
user
ID
id
to
to
this
this
item.
item
Going
going
into
into
Firebase,
firebase
we
we
can
can
see
see
that
that
the
the
member's
member's
collection
collection
on
on
this
this
item
item
It's
it's
updated
updated
with
with
the
the
key
key
after
after
each
each
button
button
is
is
clicked.
clicked
That's
that's
it
it
for
for
this
this
episode.
episode
If
if
you
you
found
found
the
the
video
video
helpful,
helpful
please
please
like
like
and
and
subscribe.
subscribe
And
and
if
if
you
you
wanna
wanna
support
support
the
the
channel,
channel
consider
consider
becoming
becoming
a
a
pro
pro
subscriber
subscriber
at
at
angularfirebase.com.
angularfirebase.com
For
for
just
just
a
a
few
few
bucks
bucks
a
a
month,
month
you'll
you'll
get
get
access
access
to
to
exclusive
exclusive
content
content
as
as
well
well
as
as
free
free
1
1
on
on
1
1
project
project
consulting.
consulting
Thanks
thanks
for
for
watching,
watching
and
and
I'll
i'll
see
see
you
you
next
next
time.
time
success
In this episode of a video discussing data privacy, the host discusses ways to associate Isabel users with data in real time and provides three ways to do so. The first method involves creating a database design that only tracks relationships and not efficiency, and the second method involves creating user relationships with obtain a user ID and setting a return parameter to the user. The third method involves modifying three lines of code to allow for the right operation to occur when a user is added to a list.
How to Connect Firebase Users to their Data - 3 Methods
In this episode, I'm going to talk about ways to associate Firebase users with data in the real time database. I'll go over 3 different methods and when to use them and also talk about ways to keep them secure on the back end. But before we get into that, it's important to go over a couple NoSQL database design best practices. If you come from a SQL background, you're used to designing a database that is completely centered around relationships. In NoSQL, we care less about relationships and more about the efficiency of the actual query that we're making. Consider a database design that has everything nested under the user ID. If you wanted to loop over just the users and display their username, You'd also have to load all the nested data further down the tree which would become very inefficient if you have a lot of records. A better way to handle this is to keep the data shallow so you only query for the actual data you need. Now we can start building some user relationships with Firebase and Angular for the first method works by nesting a user's items under their own user ID. This is ideal when you're showing data that's organized by user, for example a Twitter feed. Here we have an item service and we're going to import both the AngularFire database in the AngularFire auth module. Then we create an item class which represents our data at this point with just one body string attribute. This class will be returned to us as an AngularFire list observable. When we inject the service, we want to subscribe to the AngularFire auth state and we'll set the user ID a return from that auth state to the user ID on the service. This allows us to make queries based on the currently logged in user's ID. Now that we have the user ID, we can hit the database for the list that is nested under that user ID. If it's undefined, we just return null. If it's defined, we interpolate it into the path to the database. If we wanna create a new item, it's simple as pushing another item object onto this list observable. Back in the app, we can see that when we create a new item. It's nested under that user's user ID as expected. If we log out and log in with a different user, we shouldn't see any items in the feed, which we don't. And if we create a new item with this user, their data is nested under their user ID, essentially separating the data by user. This works great on the front end, but we also need to secure the back end data with a Firebase database rule. In this example, we match the user ID that the data is nested under with the actual auth ID in Firebase. If it's a match, then we allow the right operation to take place. A second way to associate users with items is to save the user ID as an attribute on the item itself. This approach is useful when you need the association but you're not querying by the user. For example, comments on a blog post or something similar. We can make this work by modifying 3 lines of code. First, we add a user ID to the item class. Then we set that user ID on the item when it's created. Then we remove the user ID from the database path we're no longer nesting under the user ID. Back in the app, we create a new item and we can see that user ID is on the item in itself. And if we create another item with a different user, we should have a different user ID on that item, but still both items are coming up in the feed now. In Firebase, we can see the user ID attribute is on each item. To secure the back end, we see if the existing data in the database has a matching user ID. And we also allow the right to occur if it's null because as that means the user's creating a new item. The 3rd scenario is when you have items that can be associated with multiple Users. In this case, we create a collection of members that are nested under the item ID and the key for each member is that user's user ID. Back in the service, we'll create a new function to join an item, passing it the item's key as an argument. The data will be the user ID for the key set to true. We return the current members as an object observable, then update that observable with the new data. To leave an item, we follow the same basic approach. We query for an object observable with the current user ID. And if it exists, we remove it. Back in the app, we create a new item that defaults to no members, and then we can click the join button, which will add that user ID to the list of members. When we log in with a different user, we should see that they can also add their or user ID to this item. Going into Firebase, we can see that the member's collection on this item It's updated with the key after each button is clicked. That's it for this episode. If you found the video helpful, please like and subscribe. And if you wanna support the channel, consider becoming a pro subscriber at angularfirebase.com. For just a few bucks a month, you'll get access to exclusive content as well as free 1 on 1 project consulting. Thanks for watching, and I'll see you next time.
In this episode of a video discussing data privacy, the host discusses ways to associate Isabel users with data in real time and provides three ways to do so. The first method involves creating a database design that only tracks relationships and not efficiency, and the second method involves creating user relationships with obtain a user ID and setting a return parameter to the user. The third method involves modifying three lines of code to allow for the right operation to occur when a user is added to a list.