# Save user data in Firebase

> Learn how to save user data in Firebase to enhance user experience by utilizing Cloud Firestore for user authentication and personalized data management.

https://docs.pandasuite.com/essentials/integrations/save-user-data-in-firebase/

**Save user data in Firebase (score, profile, language preference, progress...) and make user experience more personal in the app.**

[Video: Tutorial on saving user data (score, profile, language) in Firebase to personalize a PandaSuite app](https://www.youtube.com/embed/YzxsL0-JXVc?&hl=en&cc_lang_pref=en&cc_load_policy=1)

This feature relies on the authentication system via Firebase.

When a user account is created on Firebase, it has a fixed set of properties: a unique identifier, an email address, and a name that are stored in the Firebase project user database. Thanks to Cloud Firestore, the Firebase database, you can store additional properties: a score, a user profile, etc.

> **Info**
>
> Before associating any data with a user, first create a Firebase project and configure it properly via the Firebase Session component. Insert the authentication form to allow users to log into your application.

## See also

- [Build a leaderboard](https://docs.pandasuite.com/essentials/gamification/leaderboard/)

## Data structure

[Cloud Firestore](https://firebase.google.com/docs/firestore) is a NoSQL cloud database that is used to add, retrieve, and update data in your application. Basically, it is a database used to store data in your Firebase console. Firestore data is stored as documents, which are organized into *collections*. There are no tables or rows. Each *document* contains a set of key-value pairs.

The collection *users* contains your different users, each represented by a document.

Here is an example of a document:

[Image: Cloud Firestore console: a user's document in the users collection, with its email and name fields highlighted]

Cloud Firestore is schema-free, so you have complete freedom over the fields you place in each document and the types of data you store in those fields.

## Create / edit data from the app

Cloud Firestore does not require a predefined schema. However, in PandaSuite, the easiest workflow is now to define that structure in advance so Studio can expose it more clearly when you configure your app.

### 1. Prepare the user document structure

Start with **Default user data** in the [Firebase Session](https://docs.pandasuite.com/essentials/components/firebase-session/) component.

This tab defines the document that will be created in Cloud Firestore for each new user. It is the best place to prepare profile fields, progress data, preferences, nested objects, arrays, or PandaSuite collections.

Because this structure already exists in Studio, it becomes easier to target the right fields later in your bindings, conditions, and actions.

### 2. Complete the profile during sign-up

If you also need to collect information in the registration form, add it with **Advanced user fields** in the [Authentication](https://docs.pandasuite.com/essentials/components/authentication/) component.

These sign-up values are then merged into the default structure when the user account is created.

### 3. Update user data later from the app

Once the user account has a document in Cloud Firestore, select a trigger and choose **Interact with a data source** > **Session Firebase** > **Modify the data**.

[Video: On a quiz answer, setting up the Modify the data action of Session Firebase to increment the score field by 1]

> **Info**
>
> This action uses the same generic data editing field as the [Datastore](https://docs.pandasuite.com/essentials/data/datastore/), but it updates the Cloud Firestore document of the currently signed-in user.

Use it to update an existing field, increment a score, add or remove items in an array or collection, or clear a field when needed.

In this control, choose a **Target**, then a **Function**, and a **Value**
when that function requires one.

[Video: On a favorite button, setting up Modify the data to add the selected place, taken from the local database, to the saved_items list]

> **Info**
>
> Use the same field name everywhere in your project. For example, if you save
> `language` in one action, reuse exactly `language` when you read or update it
> elsewhere.
>
> If a field was not prepared in **Default user data**, it can still be added
> later when you update an existing user document from the app. This remains
> possible, but preparing the structure first usually makes configuration much
> easier in Studio.

## Manage data from the Cloud Firestore console

From the Firebase console, view, add, edit and delete your data in real-time. Click on a collection to view its documents, then click on a document to view its fields and sub-collections. Click on a field to change its value.

[Image: Cloud Firestore console in three columns: users collection, its documents, then the email, name and score fields of a user]

You can then reuse this data in your app with data binding, conditions, and automation workflows.

## Store PandaSuite-specific data types in Firebase

You can also use Firebase to store PandaSuite-specific types and easily retrieve them. This helps maintain the structure of associated data and use it for automated actions, such as resuming progress or triggering a screen change based on previously saved information.

[Video: Storing PandaSuite-specific types in Firebase and retrieving them in the app, part 1 of 2](https://www.youtube.com/embed/Kn7Mt1Ol1SU)

[Video: Storing PandaSuite-specific types in Firebase and retrieving them in the app, part 2 of 2](https://www.youtube.com/embed/IOjsOPASqqg)
