Creating a REST API using FastAPI and Firebase Authentication for Email and Password Authentication

Posted by

Building a REST API with FastAPI and Firebase Authentication

Building a REST API with FastAPI and Firebase Authentication (Email and Password Auth)

FastAPI is a modern and fast web framework for building APIs with Python. Firebase Authentication is a service that provides easy-to-use authentication for your web and mobile applications. In this article, we will go through the steps of creating a REST API with FastAPI and integrating it with Firebase Authentication using email and password authentication.

Setting up Firebase Authentication

The first step is to set up Firebase Authentication for your project. Go to the Firebase console (https://console.firebase.google.com/) and create a new project. Once your project is created, navigate to the Authentication section and enable the Email/Password sign-in method.

Installing FastAPI

Next, we need to install FastAPI. Open your terminal and run the following command:

pip install fastapi

Once FastAPI is installed, we can create a new Python file for our API code. Let’s call it main.py.

Creating the API with FastAPI

In main.py, we can define our API endpoints and their corresponding functions. Here’s a basic example of an endpoint that returns a JSON response:


   from fastapi import FastAPI

   app = FastAPI()

   @app.get("/")
   def read_root():
       return {"message": "Hello, World"}
   

Make sure to run your FastAPI server using the following command:uvicorn main:app --reload

Integrating with Firebase Authentication

Now, let’s integrate our FastAPI API with Firebase Authentication. We can use the Firebase Admin SDK for Python to verify user credentials. First, install the SDK using the following command:

pip install firebase-admin

Then, in our main.py file, we can initialize the Firebase Admin SDK and create an authentication endpoint that receives user credentials and verifies them using Firebase Authentication:


   import firebase_admin
   from firebase_admin import credentials, auth
   from fastapi import HTTPException

   cred = credentials.Certificate('path_to_your_service_account_key.json')
   firebase_admin.initialize_app(cred)

   @app.post("/login")
   def login(username: str, password: str):
       try:
           user = auth.get_user_by_email(username)
           if user:
               # Verify the password here
               return {"message": "Login successful"}
           else:
               raise HTTPException(status_code=401, detail="Invalid credentials")
       except:
           raise HTTPException(status_code=401, detail="Invalid credentials")
   

Conclusion

With FastAPI and Firebase Authentication, we can easily build a secure and modern REST API with email and password authentication. Using the Firebase Admin SDK, we can integrate our API with Firebase Authentication to securely handle user authentication and authorization. This combination of tools provides a robust and reliable solution for building authentication and authorization into our FastAPI APIs.

0 0 votes
Article Rating
9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@eddielicea3740
10 months ago

Rename schema_extra to json_schema_extra for the example values to update if you're following along with a later V.

@mrchongnoi
10 months ago

Glad I found your video. You save me some brain cells.

@eddielicea3740
10 months ago

Thank you friend! Keep up the good work!

@HungNguyen-vz7mh
10 months ago

Thanks bro. Good tutorial

@SaqibKhan-eo5ii
10 months ago

thanks bro

@cleverengineer2410
10 months ago

Amazing keep going buddy

@evitorbarros
10 months ago

Thanks for the content!

@fidelischukwunyere3142
10 months ago

Nice video… Congrats to you

@SsaliJonathan
10 months ago

Thanks for 5000 subscribers. Using Firebase Auth, you can also change your authentication to go beyond just using emails and Password. Would you love to see me make a video on that?