Skip to content

MarLops/template_fastapi_auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 

Repository files navigation

Template FastApi Auth

A template to a fastapi'project with auth, middleware and dependency injection

How to use

To create a project, you need install cookicutter

pip install cookiecutter

Next, run the command

cookiecutter https://github.com/MarLops/template_fastapi_auth.git

Secure

The project offer two types of auth

  1. Basic
  2. JWT

The project using users.json as Users'database, but this can modify.

  1. Add auth in function/endpoint

To add auth in some function/endpoint, you need add

user = Depends(get_current_user)

as one of argument. If user is not in database, the variable "user" will be None

  1. Override Database

To overrride the database, you need to acess main.py and complete the DatabaseUserPersonal class

After you complete, we will need change create_database_user in same file

def create_database_user(*arg,**kwargs):
    DB_USER = DatabaseUserPersonal()

The arguments of create_database_user can be modify in app.py

  1. Tests

There are tests

  1. unit test
  2. endpoints
  3. integration

Database

The project can providence a database

  1. Overrride Database

To overrride the Database, you need to acess main.py and complete the DB class

After you complete, you will need change create_database in same file

def create_database(*arg,**kwargs):
    DB_app = DB(*arg,**kwargs)

The arguments of create_database can be modify in app.py

  1. Add database in function/endpoint

To add database in some function/endpoint, you need add

db = Depends(get_database)

as one of argument.

  1. Tests

The test can be create in tests folders.

  1. unit
  2. endpoint
  3. integration

Endpoints

  1. Create

To create new endpoints, you can

  1. Create at app.py
  2. Create at {{cookiecutter.struct}}.py
  3. Create new file and mount in app.py

Middleware

  1. Override Middleware

To overrride the Middleware, you need to acess main.py and complete the Middware_Personalite class

After you complete, you will need change create_middware in same file

def create_middware(*arg,**kwargs):
    return Middware_Personalite()

The arguments of create_middware can be modify in app.py

The method middleware_before is call before the endpoint receive the request

The method middleware_after is call afther the endpoint receive the response

Configuration

The project use configparse. To add new configuration, modify settings.ini file .