Skip to main content

Reusable Flask authentication module with registration, login, and email verification

Project description

Flask Accounts (Reusable Auth Module)

A modular, reusable Flask authentication system with:

  • User registration
  • Login / logout
  • Email verification (with expiration + resend)
  • Password hashing (Werkzeug)
  • PostgreSQL backend
  • Config-driven email (SMTP or terminal mode)

Designed to be plug-and-play in future Flask applications using:

from app.auth import init_auth
init_auth(app)

๐Ÿš€ Quick Start

git clone https://github.com/GabeKL22/flask_accounts.git
cd flask_accounts

python -m venv venv
source venv/bin/activate

pip install -r requirements.txt

โš™๏ธ Configuration

Edit:

app/config.py

Set your values:

SECRET_KEY = "your-secret-key"

# PostgreSQL
DB_HOST = "localhost"
DB_NAME = "accountdb"
DB_USER = "accountuser"
DB_PASSWORD = "yourpassword"

# Email (SMTP)
SMTP_HOST = "smtp.gmail.com"
SMTP_PORT = 587
SMTP_USERNAME = "youremail@gmail.com"
SMTP_PASSWORD = "your_app_password"
SMTP_FROM_EMAIL = "youremail@gmail.com"

# Other useful configurations (UI)
LOGIN_REDIRECT = "home" # Must have a route defined
REGISTER_REDIRECT = "verify_email" # Must have a route defined
VERIFY_EMAIL_REDIRECT = "login" # Must have a route defined
LOGIN_BANNER = "Welcome Back"
LOGIN_BANNER_MSG = "Login to your account"
REGISTER_BANNER = "Create Account"
REGISTER_BANNER_MSG = "Register to get started"
CSS_STYLE_FILE = ""

# Dev mode
USE_TERMINAL_EMAIL = True

Dev Mode Behavior

Setting Behavior
True Prints verification code in terminal
False Sends real email via SMTP

๐Ÿ—„๏ธ PostgreSQL Database Setup

1. Open PostgreSQL

sudo -u postgres psql

2. Create database

CREATE DATABASE accountdb;

3. Create user (optional but recommended)

CREATE USER accountuser WITH PASSWORD 'yourpassword';
ALTER ROLE accountuser SET client_encoding TO 'utf8';
ALTER ROLE accountuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE accountuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE accountdb TO accountuser;

4. Run schema

From your project root:

psql -U accountuser -d accountdb -f schema.sql

โ–ถ๏ธ Run the App

python run.py

Then open:

http://<ip>/auth/register

๐Ÿ” Authentication Flow

  1. Register a new account
  2. Receive verification code (terminal or email)
  3. Verify email
  4. Login
  5. Access protected routes

๐Ÿ“ Project Structure

project/
โ”‚
โ”œโ”€โ”€ run.py
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ schema.sql
โ”‚
โ””โ”€โ”€ app/
    โ”œโ”€โ”€ __init__.py
    โ”œโ”€โ”€ config.py
    โ”œโ”€โ”€ db.py
    โ”‚
    โ””โ”€โ”€ auth/
        โ”œโ”€โ”€ __init__.py        # init_auth(app)
        โ”œโ”€โ”€ routes.py
        โ”œโ”€โ”€ service.py
        โ”œโ”€โ”€ validators.py
        โ”œโ”€โ”€ session.py
        โ”‚
        โ””โ”€โ”€ templates/
            โ””โ”€โ”€ auth/
                โ”œโ”€โ”€ login.html
                โ”œโ”€โ”€ register.html
                โ””โ”€โ”€ verify_email.html

๐Ÿ”Œ Using This in Another Flask App

1. Copy module

Copy:

app/auth/
schema.sql

into your new project.


1.1 PIP install module (from top directory)

pip install -e .

2. Add required config

Your app must define:

SECRET_KEY
DB_HOST
DB_NAME
DB_USER
DB_PASSWORD
SMTP_HOST
SMTP_PORT
SMTP_USERNAME
SMTP_PASSWORD
SMTP_FROM_EMAIL
USE_TERMINAL_EMAIL

3. Initialize auth

from flask import Flask
from app.auth import init_auth

def create_app():
    app = Flask(__name__)
    app.config.from_object(Config)

    init_auth(app)

    return app

4. Routes provided

  • /auth/register
  • /auth/login
  • /auth/logout
  • /auth/verify-email
  • /auth/resend-code

๐Ÿง  Example Protected Route

from flask import session, redirect, url_for

@app.route("/dashboard")
def dashboard():
    if "user_id" not in session:
        return redirect(url_for("auth.show_login"))
    return "Welcome to your dashboard"

โš ๏ธ Notes

  • Uses session-based authentication (no JWT)
  • Uses raw SQL via psycopg2
  • Designed for extension into SaaS applications

๐Ÿš€ Future Improvements

  • Password reset flow
  • JWT / token-based auth
  • OAuth (Google, GitHub)
  • SQLAlchemy migration
  • Packaging for pip install

๐Ÿง‘โ€๐Ÿ’ป Author

Gabriel Leffew

๐Ÿ“œ License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_accounts-0.1.1.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_accounts-0.1.1-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file flask_accounts-0.1.1.tar.gz.

File metadata

  • Download URL: flask_accounts-0.1.1.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for flask_accounts-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4ffc74c2ad6f2b371c51083f5680bb8d5f9bae7b59b51417a8cabef7c746bbbe
MD5 a18c2912264ee106b4a63efc1cf20a27
BLAKE2b-256 ada899aac9111403d670093145f3e859e618bbcaa1d409dfff2c7cf80ec2364a

See more details on using hashes here.

File details

Details for the file flask_accounts-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: flask_accounts-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for flask_accounts-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4003936043667eeb65b4360e81ba9f8df34ddcbf4562d9a4466d73e6cda4e648
MD5 033a61e32c0530247015d100e30bed89
BLAKE2b-256 b6fa2ac0c688a6786f34721f3c043ac58e3554dfa8504b29be30e49bf3b2bf76

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page