Hello,
After authenticate users with their email address and password using Firebase Authentication, I need to create a private database for the logged user. I would like to use Cloud Firestore, Realtime Database Firebase or Storage Firebase.
How can I create a private database (using Firebase) for each user using ionic angular (v5)?
let currentUser = firebase.auth().currentUser; // here I have user data. I can use currentUser.id
How could be the next step to create the Cloud Storage Database for the currentUser?
Thank you for your time.
I am new in Angular and Firebase.
How could be the function creating Storage Firebase DB?
Maybe I understand you incorrectly but I prefer to use firestore rules to secure document paths in the firestore database. And that way create what you seem to refer to as “private database for a user”.
It is a common pattern to use the uid of the user in e.g. “users” collection in combination with rules to prevent other than the user to read/write stuff.
The api reference and/or google has been my best friend here if I wanted to know how
I am fairly sure firebase functions do not create new databases. Nor can you programmatically - in firebase API - create new firestore and/or realtime databases. And even if you could, you shouldnt
Something like this (I found this reference Building a Ionic Firebase App step by step)
createTask(value){
return new Promise<any>((resolve, reject) => {
let currentUser = firebase.auth().currentUser;
**this.afs.collection('people').doc(currentUser.uid).collection('tasks').add**({
title: value.title,
description: value.description,
image: value.image
})
.then(
res => resolve(res),
err => reject(err)
)
})
}
Does this function prevent other than the user to read/write stuff?
Cannot tell - because it depends on the security rules. Out of the box: no.
read this
https://firebase.google.com/docs/firestore/security/get-started
Also consider reading AngularFire doc for examples.
And btw, your code isn’t necessarily the best because you should not need to create a Promise on your own (new Promise). The tutorial you refer to may look well intended but does this structurally incorrect
Better imho:
createTask(value){
let currentUser = firebase.auth().currentUser;
return this.afs.collection('people').doc(currentUser.uid).collection('tasks').add({
title: value.title,
description: value.description,
image: value.image
})
}
And this is a good tutorial (except on firestore rules)
https://devdactic.com/ionic-firebase-angularfire-7/
1 Like
Here I have an example from
https://firebase.google.com/docs/firestore/security/rules-query
Secure and query documents based on auth.uid
The following example demonstrates how to write a query to retrieve documents protected by a security rule. Consider a database that contains a collection of story
documents:
/stories/{storyid}
{
title: "A Great Story",
content: "Once upon a time...",
author: "some_auth_id",
published: false}
(…)
The following security rule uses the request.auth
and resource.data
variables to restrict read and write access for each story
to its author:
service cloud.firestore {
match /databases/{database}/documents {
match /stories/{storyid} {
// Only the authenticated user who authored the document can read or write
allow read, write: if request.auth != null && request.auth.uid == resource.data.author; }
}
}
The query constraints match security rules constraints:
var user = firebase.auth().currentUser;
db.collection("stories").where("author", "==", user.uid).get()
Many thanks for all comments. These rules are now more clear for me.
1 Like