๐Ÿ‘ฏUser Authentication - Firebase

Enable User Authentication and data storage with Firebase

Firebase Setup

Detailed screenshots are in the Firebase Setup troubleshooting section if you get stuck at any point. Find them here Firebase Setup

You'll need a Firebase account to set up your user authentication system and real-time database. Create a Firebase account with Google if you don't already have one.

Once you're signed in to your account, create a new Project for your application. For this project, we'll be using Authentication and Realtime Database features. You can find these on the menu bar in your Firebase console.

Create a new app in your project. During the registration process, there will be a section to add the SDK to your app. This contains the information that you will need to transfer to the .env file. If you don't find it, you can find it this way:

  • Navigate to your Project Settings

  • Find the "Your apps" section and view the code under the SDK Setup and Configuration title

  • You should see a Firebase config within the code containing the following variables:

firebaseConfig = {
  apiKey: "",
  authDomain: "",
  databaseURL: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: "",
  appId: "",
  measurementId: ""
};

The values of each of these variables will be filled out and unique to your project. We'll need these values to set up our environment variables in the following steps.

Authentication Method

Navigate to Authentication in the Firebase side menu. Click on the "Sign-in method" tab and enable Email/Password.

Realtime Database

Navigate to your real-time database in the Firebase side menu. Click on the "Data" tab and click the "+" button next to your project URL. Enter "users" as the key, and leave the value blank.

Click into the "Rules" tab, and configure read and write permissions for your users:

{
  "rules": {
    "users": {
      ".read": true,
      ".write": true,
      ".indexOn": "email",
    }
  }
}

THIS IS NOT SECURE AND ONLY MEANS FOR TESTING. YOU SHOULD ENABLE VERY STRICT SECURITY WHEN DEPLOYING TO PRODUCTION. Read more here: Firebase Database Security

The .indexOn rule allows to search for a specific user. When PySaaS looks up a user, it filters on the email and Firebase requires a rule to allow the database to index on that column.

Firebase Environment Variables

Gather the values from the previous step and match them to the following in your .env file:

# Firebase
FIREBASE_API_KEY=
FIREBASE_AUTH_DOMAIN=
FIREBASE_DB_URL=
FIREBASE_PROJECT_ID=
FIREBASE_STORAGE_BUCKET=
FIREBASE_MSG_SENDER_ID=
FIREBASE_APP_ID=
FIREBASE_MEASUREMENT_ID=

Last updated