Firebase Authentication with Flask Backend

Barandev
 from flask import Flask, render_template, session, redirect, request
from dotenv import load_dotenv
import os

app = Flask(__name__)

@app.route("/firebase_config")
def firebase_config():
    firebase_config = {
        "apiKey": os.environ.get("FIREBASE_API_KEY"),
        "authDomain": os.environ.get("FIREBASE_AUTH_DOMAIN"),
        "databaseURL": os.environ.get("FIREBASE_DATABASE_URL"),
        "projectId": os.environ.get("FIREBASE_PROJECT_ID"),
        "storageBucket": os.environ.get("FIREBASE_STORAGE_BUCKET"),
        "messagingSenderId": os.environ.get("FIREBASE_MESSAGING_SENDER_ID"),
        "appId": os.environ.get("FIREBASE_APP_ID"),
        "measurementId": os.environ.get("FIREBASE_MEASUREMENT_ID")
    }
    return firebase_config

# Set a secret key for session support
app.secret_key = 'your_secret_key'

@app.route("/set_session", methods=['POST'])

About this template

This application integrates Firebase authentication with a Flask backend. The firebaseconfig.js file initializes Firebase using the provided configuration fetched from the backend. It exports the initialized Firebase app and authentication object. The signedin.js file handles sign-in functionality on the client-side. It retrieves user information from session storage upon page load, displays it, and redirects to the sign-in page if the information is not found. It also handles sign-in and sign-out actions, making requests to the backend for authentication. The backend then interacts with Firebase to authenticate users, store user data in session, and handle sign-out requests. Made by BaranDev[https://github.com/BaranDev]

Introduction to the Firebase Sign Up and Login Template

This template provides a complete authentication system for your web application, integrating Firebase authentication with a Flask backend. It includes sign-up and sign-in functionality, session management, and user interface templates for both signing in and signing up. The template is designed to be easy to use, even for non-technical builders, and it handles all the deployment details for you.

Getting Started with the Template

To begin using this template, simply click on "Start with this Template" in the Lazy builder interface. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code.

Initial Setup

Before testing the application, you'll need to set up some environment secrets within the Lazy Builder. These secrets will store your Firebase project's configuration details securely.

  • Navigate to the Environment Secrets tab within the Lazy Builder.
  • Set the following secrets with the corresponding values from your Firebase project settings:
    • FIREBASE_API_KEY
    • FIREBASE_AUTH_DOMAIN
    • FIREBASE_DATABASE_URL
    • FIREBASE_PROJECT_ID
    • FIREBASE_STORAGE_BUCKET
    • FIREBASE_MESSAGING_SENDER_ID
    • FIREBASE_APP_ID
    • FIREBASE_MEASUREMENT_ID
  • These values can be found in your Firebase project's settings in the Firebase console.

Test: Pressing the Test Button

Once you have set up the environment secrets, you can test the application by clicking the "Test" button in the Lazy builder interface. This will deploy the app and launch the Lazy CLI.

Using the App

After deployment, the Lazy builder CLI will provide you with a dedicated server link to access your web application. You can use this link to interact with the sign-up and sign-in pages directly in your browser.

If you need to make a POST request to the '/set_session' endpoint, here's a sample request you might use:


fetch('/set_session', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({ email: 'user@example.com', uid: 'user_id' })
});

And a sample response might look like this:


'User data stored in session.'

Integrating the App

If you wish to integrate this authentication system into another service or frontend, you can use the provided server link to make requests to the sign-up and sign-in endpoints. Additionally, you can embed the sign-up and sign-in forms into your existing web pages by including the HTML and JavaScript code provided in the template.

For example, to integrate the sign-up form, include the following code snippet in your HTML file:


<div class="container">
    <h1>Sign Up</h1>
    <form id="signup-form">
        <input type="email" id="user-email" placeholder="Email" required>
        <input type="password" id="user-password" placeholder="Password" required>
        <button type="submit">Sign Up</button>
    </form>
    <a href="/">Go Back</a>
</div>

Remember to also include the corresponding JavaScript file to handle the form submission and interaction with Firebase:


<script src="/static/js/signup.js" type="module"></script>

By following these steps, you can easily set up a secure and functional authentication system for your web application using the Firebase Sign Up and Login Template on the Lazy platform.

Category
Technology
Last published
July 26, 2024

More templates like this

SecureUserAuthenticator

Develop a secure User Authentication system for users to register, log in, and manage their profiles, laying the foundation for user-specific data management and permissions in the CMS.

Laravel
Python
Flask
Javascript

FALLBACK | Flask, HTML, JS and Tailwind Based Website

This is a good starting point for styled website. It has a header, footer. Has Tailwind and Flowbite loaded so you can build nice looking pages from here.

Flask
HTML

MP3ify: Youtube to MP3 Converter

A web application that allows users to download YouTube videos from URLs and provides the option to convert them to MP3 format.

Python
Flask
Home
/
Firebase Authentication with Flask Backend