# Firebase Storage - private database after Auth

**URL:** <https://forum.ionicframework.com/t/firebase-storage-private-database-after-auth/218144>\
**Category:** Ionic Angular\
**Created:** [December 7, 2021, 8:11pm UTC](https://forum.ionicframework.com/t/firebase-storage-private-database-after-auth/218144 "2021-12-07T20:11:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![abuabara](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@abuabara](https://forum.ionicframework.com/u/abuabara)\
**Post date:** [December 7, 2021, 8:11pm UTC](https://forum.ionicframework.com/t/firebase-storage-private-database-after-auth/218144/1 "2021-12-07T20:11:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mikrochipkid](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mikrochipkid/32/116956_2.png) [@mikrochipkid](https://forum.ionicframework.com/u/mikrochipkid)\
**Post date:** [December 7, 2021, 9:06pm UTC](https://forum.ionicframework.com/t/firebase-storage-private-database-after-auth/218144/2 "2021-12-07T21:06:02Z")

</div>

[https://firebase.google.com/docs/reference/js](https://firebase.google.com/docs/reference/js)

---

<div class="post-metadata">

**Author:** ![abuabara](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@abuabara](https://forum.ionicframework.com/u/abuabara)\
**Post date:** [December 7, 2021, 9:40pm UTC](https://forum.ionicframework.com/t/firebase-storage-private-database-after-auth/218144/3 "2021-12-07T21:40:34Z")

</div>

I am new in Angular and Firebase.  
How could be the function creating Storage Firebase DB?

---

<div class="post-metadata">

**Author:** ![Tommertom](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Tommertom](https://forum.ionicframework.com/u/Tommertom)\
**Post date:** [December 7, 2021, 9:51pm UTC](https://forum.ionicframework.com/t/firebase-storage-private-database-after-auth/218144/4 "2021-12-07T21:51:15Z")

</div>

> [@abuabara](#):
>
> How can I create a private database (using Firebase) for each user using ionic angular (v5)?

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

---

<div class="post-metadata">

**Author:** ![abuabara](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@abuabara](https://forum.ionicframework.com/u/abuabara)\
**Post date:** [December 7, 2021, 11:01pm UTC](https://forum.ionicframework.com/t/firebase-storage-private-database-after-auth/218144/5 "2021-12-07T23:01:49Z")

</div>

Something like this (I found this reference [Building a Ionic Firebase App step by step](https://ionicthemes.com/tutorials/building-a-ionic-firebase-app-step-by-step))

```auto
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?

---

<div class="post-metadata">

**Author:** ![Tommertom](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Tommertom](https://forum.ionicframework.com/u/Tommertom)\
**Post date:** [December 8, 2021, 6:18am UTC](https://forum.ionicframework.com/t/firebase-storage-private-database-after-auth/218144/6 "2021-12-08T06:18:33Z")

</div>

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](https://firebase.google.com/docs/firestore/security/get-started)

Also consider reading AngularFire doc for examples.

> **[GitHub - angular/angularfire: The official Angular library for Firebase.](https://github.com/angular/angularfire)**
>
> The official Angular library for Firebase. Contribute to angular/angularfire development by creating an account on GitHub.

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:

```auto
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/](https://devdactic.com/ionic-firebase-angularfire-7/)

---

<div class="post-metadata">

**Author:** ![abuabara](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@abuabara](https://forum.ionicframework.com/u/abuabara)\
**Post date:** [December 8, 2021, 9:59am UTC](https://forum.ionicframework.com/t/firebase-storage-private-database-after-auth/218144/7 "2021-12-08T09:59:12Z")

</div>

Here I have an example from

> **[Securely query data  |  Firestore  |  Firebase](https://firebase.google.com/docs/firestore/security/rules-query)**
>
> A guide to securely querying data in Cloud Firestore using security rules.

### 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}**

```auto
{  
title: "A Great Story",  
content: "Once upon a time...",  
author: "some_auth_id",  
published: false}

```

(…)  
The following security rule uses the [`request.auth`](https://firebase.google.com/docs/reference/rules/rules.firestore.Request) and [`resource.data`](https://firebase.google.com/docs/reference/rules/rules.firestore.Resource) variables to restrict read and write access for each `story` to its author:

```auto
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:

```auto
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.
