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"

# 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.0.tar.gz (12.6 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.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_accounts-0.1.0.tar.gz
  • Upload date:
  • Size: 12.6 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.0.tar.gz
Algorithm Hash digest
SHA256 f5dc0dda8a4accf31972d69e58d194ac751b9a6d3924bd3bb51e1cbb57c6945f
MD5 1d74a456934529f4e5e3e2175790b44f
BLAKE2b-256 0f5f1d7f65f6d3e508982afd7ccf722b9e6a66aa8e51018e90215258fd4eef68

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flask_accounts-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.0 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f7f4e5239b03a1732485b73cdb1d18316bfebc5cfecc5c9214476833641931ba
MD5 90c990d0bfa0fe6ad3a41ab3bbc1014e
BLAKE2b-256 7185f381fa917304de0e441951aa9e409e750882cab16c9e8cab0d950b3579a8

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