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 Flask applications using:

from flask_accounts 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"
AUTH_CUSTOM_CSS = "custom.css" # Must live inside of a static folder

# 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/
โ”‚
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ schema.sql
โ”‚
โ””โ”€โ”€ flask_accounts/
    โ”œโ”€โ”€ __init__.py
    โ”œโ”€โ”€ config.py
    โ”œโ”€โ”€ db.py
    โ”‚
    โ””โ”€โ”€ auth/
        โ”œโ”€โ”€ __init__.py        # init_auth(app)
        โ”œโ”€โ”€ routes.py
        โ”œโ”€โ”€ service.py
        โ”œโ”€โ”€ validators.py
        โ”œโ”€โ”€ session.py
        โ”‚
        โ”œโ”€โ”€ static/
        โ”‚   โ”œโ”€โ”€ auth.css
        โ”‚
        โ””โ”€โ”€ templates/
            โ””โ”€โ”€ auth/
                โ”œโ”€โ”€ login.html
                โ”œโ”€โ”€ register.html
                โ””โ”€โ”€ verify_email.html

๐Ÿ”Œ Using This in Another Flask App

1a. Install module

Run:

pip install flask_accounts

Done.


1b. Copy module

Copy:

app/auth/
schema.sql

into your new project.


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 flask_accounts import init_auth

class Config:
    # Your Configuration (Required)

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

๐Ÿง‘โ€๐Ÿ’ป 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.2.tar.gz (11.8 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.2-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_accounts-0.1.2.tar.gz
  • Upload date:
  • Size: 11.8 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.2.tar.gz
Algorithm Hash digest
SHA256 513fd60e49d4e45c7aad550d9aba4f182f68c456e219c3ed352f027562b09e38
MD5 805777e88f6f6c1e602c93b32d189c13
BLAKE2b-256 7a8bea03c5f805d7d1817d8772d91427e5e47eaba49256233026bfed2e131ac0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flask_accounts-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.4 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3388875c165a8e1969e032c6fa5cedb4c262375ff3c649018a85fd7a97c7167b
MD5 fd7322e9c691d07f0ee92e40784229c1
BLAKE2b-256 4d855940cd590aeaf0b241cf90e6bc4202957e009bac636d66c8fa4d7966b831

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