MCPcopy Create free account
hub / github.com/TextGeneratorio/text-generator.io / api_current_user

Function api_current_user

main.py:1370–1414  ·  view source on GitHub ↗

Get current user information

(request: Request, db: Session = Depends(get_db))

Source from the content-addressed store, hash-verified

1368
1369@app.get("/api/current-user")
1370async def api_current_user(request: Request, db: Session = Depends(get_db)):
1371 """Get current user information"""
1372 if USE_POSTGRES:
1373 try:
1374 user = get_current_user(request, db)
1375 if not user:
1376 raise HTTPException(status_code=401, detail="Not authenticated")
1377
1378 # Return basic user info immediately, skip Stripe validation to prevent blocking
1379 # Stripe validation can be done asynchronously in the background if needed
1380 user_dict = user.to_dict()
1381
1382 # Only check subscription status if user has a Stripe ID
1383 if user.stripe_id:
1384 try:
1385 # Add timeout to prevent blocking
1386 subscription_item_id = await asyncio.wait_for(
1387 get_subscription_item_id_for_user_email_async(user.email), timeout=3.0
1388 )
1389 user_dict["is_subscribed"] = subscription_item_id is not None
1390 except asyncio.TimeoutError:
1391 logger.warning(f"Subscription check timed out for user {user.email}")
1392 user_dict["is_subscribed"] = False
1393 except Exception as e:
1394 logger.error(f"Error checking subscription for user {user.email}: {e}")
1395 user_dict["is_subscribed"] = False
1396 else:
1397 user_dict["is_subscribed"] = False
1398
1399 # Set default values for optional fields
1400 user_dict["num_self_hosted_instances"] = 0
1401
1402 return JSONResponse(user_dict)
1403 except HTTPException:
1404 raise
1405 except Exception as e:
1406 logger.error(f"Error in current-user endpoint: {e}")
1407 raise HTTPException(status_code=401, detail="Not authenticated")
1408 else:
1409 # Fallback to simple session checking
1410 session_secret = request.cookies.get("session_secret")
1411 if session_secret and session_secret in active_sessions:
1412 return JSONResponse(active_sessions[session_secret])
1413 else:
1414 raise HTTPException(status_code=401, detail="Not authenticated")
1415
1416
1417@app.get("/api/subscription-status")

Callers

nothing calls this directly

Calls 4

get_current_userFunction · 0.90
to_dictMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected