@@ -8,6 +8,7 @@ Step by step guide to integrate `hotosm-auth` in your project.
88| -----------| ---------------------| ------------------|
99| ** FastAPI** | [ Simple] ( #fastapi-simple-integration ) | [ With Mapping] ( #fastapi-integration-with-mapping ) |
1010| ** Django** | [ Simple] ( #django-simple-integration ) | [ With Mapping] ( #django-integration-with-mapping ) |
11+ | ** Litestar** | [ Simple] ( #litestar-simple-integration ) | [ With Mapping] ( #litestar-integration-with-mapping ) |
1112| ** Frontend** | [ All] ( #frontend-all ) | [ All] ( #frontend-all ) |
1213
1314---
@@ -57,7 +58,8 @@ async def lifespan(app: FastAPI):
5758app = FastAPI(lifespan = lifespan)
5859
5960# Mount OSM OAuth routes (optional)
60- app.include_router(osm_router, prefix = " /api/auth/osm" )
61+ # router already has prefix="/auth/osm" → routes: /api/auth/osm/login, /api/auth/osm/callback
62+ app.include_router(osm_router, prefix = " /api" )
6163```
6264
6365### Step 3: Protect routes
@@ -233,6 +235,102 @@ if getattr(settings, 'AUTH_PROVIDER', 'legacy') == 'hanko':
233235
234236---
235237
238+ ## Litestar: Simple Integration
239+
240+ For apps ** without legacy auth** (e.g.: Field-TM).
241+
242+ ### Step 1: Dependency
243+
244+ ``` toml
245+ # pyproject.toml
246+ dependencies = [
247+ " hotosm-auth[litestar]==0.2.10" ,
248+ ]
249+ ```
250+
251+ ### Step 2: Initialization
252+
253+ ``` python
254+ # main.py
255+ from litestar import Litestar
256+ from hotosm_auth_litestar import setup_auth
257+
258+ # setup_auth() loads config from env, returns (deps, route_handlers)
259+ deps, route_handlers = setup_auth()
260+
261+ app = Litestar(route_handlers = route_handlers, dependencies = deps)
262+ ```
263+
264+ ### Step 3: Protect routes
265+
266+ ``` python
267+ from litestar import get
268+ from hotosm_auth_litestar import AuthContext, OptionalAuthContext
269+
270+ @get (" /me" )
271+ async def me (auth : AuthContext) -> dict :
272+ """ Requires authentication."""
273+ return {" user_id" : auth.user.id, " email" : auth.user.email}
274+
275+ @get (" /public" )
276+ async def public (optional_auth : OptionalAuthContext) -> dict :
277+ """ Optional auth."""
278+ return {" user" : optional_auth.user.email if optional_auth.user else " anonymous" }
279+ ```
280+
281+ ### Step 4: Environment variables
282+
283+ ``` bash
284+ HANKO_API_URL=https://login.hotosm.org
285+ COOKIE_SECRET=your-32-byte-secret
286+
287+ # Only if using OSM OAuth
288+ OSM_CLIENT_ID=your-client-id
289+ OSM_CLIENT_SECRET=your-client-secret
290+ ```
291+
292+ ---
293+
294+ ## Litestar: Integration with Mapping
295+
296+ ### Steps 1-2: Same as Simple
297+
298+ ### Step 3: Custom auth dependency
299+
300+ ``` python
301+ # auth_deps.py
302+ from litestar import Request
303+ from hotosm_auth_litestar import get_current_user, get_mapped_user_id
304+
305+ async def login_required (request : Request):
306+ hanko_user = await get_current_user(request)
307+ db = request.app.state.db
308+ user_id = await get_mapped_user_id(
309+ hanko_user = hanko_user,
310+ db_conn = db,
311+ app_name = " my-app" ,
312+ auto_create = True ,
313+ email_lookup_fn = lookup_user_by_email,
314+ user_creator_fn = create_app_user,
315+ )
316+ return await get_user_by_id(db, user_id)
317+ ```
318+
319+ ### Step 4: Admin routes (optional)
320+
321+ ``` python
322+ # main.py
323+ from hotosm_auth_litestar import create_admin_mappings_router
324+
325+ admin_router = create_admin_mappings_router(
326+ get_db, app_name = " my-app"
327+ )
328+ deps, route_handlers = setup_auth()
329+ app = Litestar(route_handlers = [* route_handlers, admin_router], dependencies = deps)
330+ ```
331+
332+ ---
333+
236334## Frontend (all)
237335
238336``` tsx
@@ -255,11 +353,11 @@ VITE_HANKO_URL=https://login.hotosm.org
255353
256354## Checklist
257355
258- | Step | FastAPI Simple | FastAPI+Mapping | Django Simple | Django+Mapping |
259- | ------| ----------------| -----------------| ---------------| ----------------|
260- | Dependency | ✓ | ✓ | ✓ | ✓ |
261- | init_auth / middleware | ✓ | ✓ | ✓ | ✓ |
262- | Protect routes | CurrentUser | Override login_required | request.hotosm | request.hotosm |
263- | Helper functions | - | ✓ | - | ✓ |
264- | Admin routes | - | Optional | - | Optional |
265- | AUTH_PROVIDER env | - | ✓ | - | ✓ |
356+ | Step | FastAPI Simple | FastAPI+Mapping | Django Simple | Django+Mapping | Litestar Simple | Litestar+Mapping |
357+ | ------| ----------------| -----------------| ---------------| ----------------| ----------------- | ------------------ |
358+ | Dependency | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
359+ | Init | init_auth | init_auth | middleware | middleware | setup_auth() | setup_auth() |
360+ | Protect routes | CurrentUser | Override login_required | request.hotosm | request.hotosm | AuthContext | Custom dep |
361+ | Helper functions | - | ✓ | - | ✓ | - | ✓ |
362+ | Admin routes | - | Optional | - | Optional | - | Optional |
363+ | AUTH_PROVIDER env | - | ✓ | - | ✓ | - | ✓ |
0 commit comments