I've a small Angular application deployed to Firebase. There're only 2 users. The users are authenticated with their emails addresses. I'm able to get the user data in my Angular application.
In the development phase I've used the following Realtime Database rule:
{
"rules": {
".read": true,
".write": true
}
}
Naturally, I'd like to make the full database only readable and writeable for that 2 users.
I've tried to set up the following rules:
{
"rules": {
".read": "auth.uid !== null",
".write": "auth.uid !== null"
}
}
As expected, I receive a 401 error.
My question would be, that how should I send the uid from within the Angular app endpoint requests?
I've tried sending it via path param, like:
this.http.get(`https://myappname.firebaseio.com/data.json?auth=${uid}`).toPromise();
But passing the logged in user's uid like this won't work. Should I send it with http headers? If yes, what should be the proper syntax?
Or do I need to send anything? Maybe I just need a better realtime database rule?
Edit:
I was able to retrieve the tokenId with the auth service's constructor.
this.firebaseAuth.authState.subscribe((user: any) => {
if (user) {
this.userData = user;
localStorage.setItem('user', JSON.stringify(this.userData));
JSON.parse(localStorage.getItem('user'));
user.getIdToken().then((idToken: any) => {
this.idToken = idToken;
});
} else {
localStorage.setItem('user', null);
JSON.parse(localStorage.getItem('user'));
}
});
Now when I call an API endpoint I just simply add the idToken as a path param, like this:
return this.http.get(`https://myapp.firebaseio.com/data.json?auth=${token}`).toPromise();
And Realtime Database rule looks like this:
{
"rules": {
".read": "auth !== null",
".write": "auth !== null"
}
}