Skip to content

Simple OAuth2 with Password and Bearer

from justapi import JustAPIApp, Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
app = JustAPIApp()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
# Fake user database
fake_users_db = {
"alice": {"user_id": 1, "name": "Alice", "password": "secret"},
}
def authenticate_user(username: str, password: str):
user = fake_users_db.get(username)
if user and user["password"] == password:
return user
return None
@app.post("/token")
def login(username: str, password: str):
user = authenticate_user(username, password)
if not user:
raise HTTPException(status_code=401, detail="Invalid credentials")
return {"access_token": "fake-jwt-token", "token_type": "bearer"}