Coder Social home page Coder Social logo

rohanpudasaini / library_management_system_fastapi Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 260 KB

Simple Library management system to learn FastAPI with JWT token and RBAC.

Python 99.12% Mako 0.68% Dockerfile 0.20%
fastapi postgresql psycopg2 pydantic python sqlalchemy uvicorn jwt-authentication pyjwt cronjob

library_management_system_fastapi's Introduction

Library Management System Using FASTAPI

Library Management System

Simple Library management system to learn FastAPI

This is a simple library management system's api created using FastAPI for the endpoint, SQLAlchemy for ORM and PostgreSQL for the backend database.

How to run this code?

  1. Clone the repo and move to the file

    Clone this repo or you can download the zip file from above. To clone using terminal, go to your required file loation and open terminal there, then enter following commands.

       git clone https://github.com/Rohanpudasaini/library_management_system_fastAPI.git

    This will create make a directory with name library_management_system_fastAPI. Now we need to move inside the folder, i.e

    cd library_management_system_fastAPI
  2. Create a .env file The file must follow this format

    host=database_host
    database=database_name
    user=database_username
    password=database_password
    secret=AccessToken_secret_key
    algorithm=Token_algorithm
    secret_refresh = RefreshToken_secret_key
  3. Install requirements

    Run pip install -r requirements.txt

  4. Run the script

    Run the script with uvicorn main:app --reload

    The --reload flag as the name suggest will watch your file for change and reload the endpoint. One of the important flag is --host 0.0.0.0 that will host the endpoint to your local ip.

Docs

To know more about the endpoint and expected format you can got to localhost/docs(that is if you are hosting the api in your localhost) You can also use that swaager API docs to test the api.

Login and Authorization

I have use simple JWT token i.e Access token and Refresh token. You need to login as an librarian to get access to the access and refresh token at /login endpoint with post method with the email and password in body. Curently the access token is set to expire in 20 minutes and the refresh token is valid till about one week. Once you have sucessfully login, you need to save the both access refresh token. Once the access token expires, you need to hit the /refresh endpoint with get method and the saved refresh token as the query parameter, i.e /refresh?refreshToken=savedrefreshtoken.

In this way you can have prescistance login for a librarian.

Access the protected route/endpoints

Once you got access to the access token, you need to send it in each requests header as a bearer token. Here is a sample curl command with dummy access token.

curl -X 'GET' \
'http://localhost:8000/user/test' \
-H 'accept: application/json' \
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySUQiOiJhZG1pbkBsbXMuY29tIiwiZXhwaXJ5IjoxNzEwMTMyMDk2LjAzODc5MX0.Hz1JLerxq8fmsDL0U82m7DU1l8e04QPIzfpJJs8jKE4'

And it will return a JSON data as

{
  "User": {
    "user_details": {
      "username": "Test",
      "expiry_date": "2024-04-28T00:00:00",
      "fine": 0,
      "id": 1,
      "email": "email1",
      "date_created": "2024-02-28T00:00:00",
      "address": "address",
      "phone_number": 5678910111213
    }
  }
}

TODO

  • Add option to update or remove members and books or magazines.
  • Testing
  • User login (Maybe)

Conclusion

This is a simple FastApi example and it is nowhere near complete, This is created with sole purpose of understanding the fast api, router and authentication using JWT token.

library_management_system_fastapi's People

Contributors

rohanpudasaini avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

gkunwar059

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.