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
- Register a new account
- Receive verification code (terminal or email)
- Verify email
- Login
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ffc74c2ad6f2b371c51083f5680bb8d5f9bae7b59b51417a8cabef7c746bbbe
|
|
| MD5 |
a18c2912264ee106b4a63efc1cf20a27
|
|
| BLAKE2b-256 |
ada899aac9111403d670093145f3e859e618bbcaa1d409dfff2c7cf80ec2364a
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4003936043667eeb65b4360e81ba9f8df34ddcbf4562d9a4466d73e6cda4e648
|
|
| MD5 |
033a61e32c0530247015d100e30bed89
|
|
| BLAKE2b-256 |
b6fa2ac0c688a6786f34721f3c043ac58e3554dfa8504b29be30e49bf3b2bf76
|