Giter VIP home page Giter VIP logo

fastapi-auth's Introduction

FastAPI Full-Stack Auth Demo

This repo is a full-stack app using the FastAPI web framework, MySQL for a database, and REST to interact with a simple users table. You may install MySQL natively or use the included Docker file to create a database in a containter. The init-db.sql defines a simple schema for use with the example.

Screenshot

Prerequesites

You just need to have Python 3.7+ installed (3.10+ for the simplified typehinting).

TODO

  1. Switch to sending username/password in header! (https://fastapi.tiangolo.com/advanced/security/http-basic-auth/)
  2. Switch to making the authenticate function a dependency: (https://fastapi.tiangolo.com/az/tutorial/security/simple-oauth2)

Usage

  1. Create database credentials (specify your own USERNAME, PASSWORD, ROOT_PASSWORD, and SESSION_SECRET_KEY!)

    echo "MYSQL_HOST=localhost" > credentials.env
    echo "MYSQL_DATABASE=ece140" >> credentials.env
    echo "MYSQL_USER=USERNAME" >> credentials.env
    echo "MYSQL_PASSWORD=PASSWORD" >> credentials.env
    echo "MYSQL_ROOT_PASSWORD=ROOT_PASSWORD" >> credentials.env
    echo "SESSION_KEY=SESSION_SECRET_KEY" >> credentials.env
  2. If you have Docker installed, run the following command to start up a MySQL server (ignore this step if you already have MySQL installed and modify the credentials.env file to match your database credentials!). Run the command in a separate terminal window as it will lock up the window for logging.

    docker compose up --build &

    Note: Just closing the terminal will not stop the database server. In order to stop it, you must issue the command docker-compose down when you are finished.

  3. Create a Python virtual environment

    python3 -m venv env
  4. Start the virtual environment

    source env/bin/activate
  5. Install dependencies

    pip install -U pip
    pip install -r requirements.txt
  6. Seed the database

    cd server
    python seed-users.py
  7. Run the server

    python main.py

    or

    uvicorn main:app --reload
  8. Visit the docs page to access the login/logout POST routes

    http://127.0.0.1:8000/docs
  9. Once logged in, you can visit the protected route to see if you're logged in

    http://127.0.0.1:8000/protected

fastapi-auth's People

Contributors

ramujin avatar

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.