Add OAuth login to your FastAPI app in one command.
Project description
Add "Login with GitHub/Google" to your FastAPI app in one command.
The problem
Every time you add "Login with GitHub" to a FastAPI app, you end up:
- Googling "fastapi oauth2 example" for the 5th time
- Copy-pasting from 3 different Stack Overflow answers
- Debugging redirect URIs for 45 minutes
- Pulling in a 10,000-line auth library for a 50-line feature
The fix
pip install oauth-for-dummies
cd your-fastapi-project
oauth-init
That drops three files into your project:
| File | What it does |
|---|---|
oauth_config.py |
Loads GitHub/Google credentials from .env |
oauth_routes.py |
/auth/{provider}/login, /auth/{provider}/callback, /auth/logout |
.env |
Template with all the env vars you need |
Add two lines to your existing app:
from oauth_routes import router as oauth_router
app.include_router(oauth_router)
Done. Your users can now log in with GitHub and Google.
Quickstart
# Install
pip install oauth-for-dummies
# Scaffold into your project
cd my-fastapi-project
oauth-init
# Install deps the generated code needs
pip install fastapi uvicorn httpx python-dotenv
# Add your OAuth keys to .env, then run
uvicorn oauth_example_app:app --reload
Open http://localhost:8000 and click login.
Don't have OAuth keys yet?
GitHub: github.com/settings/developers → New OAuth App → callback URL: http://localhost:8000/auth/github/callback
Google: console.cloud.google.com/apis/credentials → Create OAuth Client ID → redirect URI: http://localhost:8000/auth/google/callback
What you get
Routes
| Route | What it does |
|---|---|
/auth/github/login |
Redirects to GitHub OAuth |
/auth/github/callback |
Handles the redirect back |
/auth/google/login |
Redirects to Google OAuth |
/auth/google/callback |
Handles the redirect back |
/auth/logout |
Clears session, redirects home |
Session helper
from oauth_routes import get_session
@app.get("/dashboard")
async def dashboard(request: Request):
user = get_session(request)
if not user:
return RedirectResponse("/auth/github/login")
return {"hello": user["name"]}
get_session() returns a dict with id, name, email, avatar, provider — or None if not logged in.
CLI options
oauth-init # all providers + example app
oauth-init --provider github # GitHub only
oauth-init --provider google # Google only
oauth-init --no-example # skip example app, just the routes + config
oauth-init --dir ./myproject # target a different directory
How OAuth works (the short version)
You click "Login with GitHub"
|
v
Your app redirects to GitHub's /authorize endpoint
|
v
User clicks "Allow" on GitHub
|
v
GitHub redirects back to /auth/github/callback?code=abc123
|
v
Your app exchanges that code for an access token (server-to-server)
|
v
Your app calls GitHub's API with that token to get the user's profile
|
v
Session created. User is logged in. No password was ever shared.
The generated code is ~150 lines total across two files. No magic. Read it.
Want to understand OAuth deeply?
This repo also includes a full tutorial app with step-by-step logging of every OAuth step:
git clone https://github.com/pranavkumaarofficial/oauth-for-dummies.git
cd oauth-for-dummies
pip install -r requirements.txt
cp .env.example .env
# add your OAuth keys
uvicorn app.main:app --reload
Every step of the OAuth flow gets printed to your terminal with clear labels.
See also:
- How OAuth Works — visual explainer
- Step-by-step Tutorial — build it from scratch
Project structure
oauth-for-dummies/
|
|-- oauth_for_dummies/ # The pip-installable CLI package
| |-- cli.py # oauth-init command
| +-- scaffold/ # Files dropped into your project
| |-- oauth_config.py
| |-- oauth_routes.py
| |-- oauth_example_app.py
| +-- dot_env_example
|
|-- app/ # Full tutorial app (learning resource)
| |-- main.py
| |-- config.py
| |-- auth/
| | |-- routes.py
| | +-- storage.py
| |-- templates/
| +-- static/
|
|-- providers/ # OAuth provider implementations
| |-- base.py # OAuthProvider base class
| |-- github.py
| |-- google.py
| +-- registry.py
|
|-- docs/ # Guides and diagrams
|-- tests/ # Unit tests
+-- pyproject.toml # PyPI packaging config
Contributing
Want to add a provider? Improve the CLI? Fix something?
See CONTRIBUTING.md.
License
MIT
Project details
Release history Release notifications | RSS feed
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 oauth_for_dummies-1.0.0.tar.gz.
File metadata
- Download URL: oauth_for_dummies-1.0.0.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4258a56fe2a50a68bdb9a5a25bf400387b6c5dca63f7a9e5f919c8d5bff54cc6
|
|
| MD5 |
88500a698652523dee3e374f6b5983a0
|
|
| BLAKE2b-256 |
1a6d7562b1999d123f4891c74c1a0f1bbf2b73ab8cbfa691d8eff3f41cb2af51
|
File details
Details for the file oauth_for_dummies-1.0.0-py3-none-any.whl.
File metadata
- Download URL: oauth_for_dummies-1.0.0-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb1e03cdcc8b238506d06ff5c712ece9837a6bc8e27ec0b48e4df7826e6c2a89
|
|
| MD5 |
074276736487eabc9df06197385524f8
|
|
| BLAKE2b-256 |
e83ff00502fd42a9afe8a24e02d901b45711e17daeceae1c68e71903b63bd580
|