This is originally taken from the wonderful article from RealPython I don't take any credit for it, just adapted some of the code to more of my likings.
Mostly I switched to Flask CLI instead of Flask-Script.
The project itself showcases how to implement a basic auth system using JWT tokens.
It basically provides a simple server that can:
- Register users
- Login users
- Generate JWT token for registered users
- Get user info assuming a valid JWT token is provided
It's a good way to understand how JWT works, although this example is not taking implementing a real Oauth dance using JWT tokens, which would be nice but would require more actors in place:
- Resource Server
- Authorization Server
- Client
Maybe a good idea for another test
- Create and activate a virtual environment
- Install the requirements
pip install -r requirements.txt
Update project/server/config.py, and then run:
$ export APP_SETTINGS="project.server.config.DevelopmentConfig"
or
$ export APP_SETTINGS="project.server.config.ProductionConfig"
Alternatively, you can use the .flaskenv file to set environment variables too
Create the databases in psql
:
$ psql
# create database flask_jwt_auth
# create database flask_jwt_auth_testing
# \q
Create the tables and run the migrations:
$ flask create_db
$ flask db init
$ flask db migrate
$ flask run
So access the application at the address http://localhost:5000/
Without coverage:
$ flask test
With coverage:
$ flask cov