From 5d42cd43835918a6e6e92b7b1ef9a078b51f0cf2 Mon Sep 17 00:00:00 2001 From: Chris Christiansen Date: Fri, 5 Jun 2026 00:57:04 +0000 Subject: [PATCH] feat(auth): add Google OAuth routes for CostGuard billing --- main.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/main.py b/main.py index c576f95..7eea25b 100644 --- a/main.py +++ b/main.py @@ -93,6 +93,39 @@ oauth.register( } ) + +@app.get('/auth/login') +async def login(request: Request): + """Redirects user to Google's OAuth 2.0 login page.""" + redirect_uri = "https://opax.vauco.no/auth/callback" + return await oauth.google.authorize_redirect(request, redirect_uri) + +@app.get('/auth/callback', name='auth') +async def auth(request: Request): + """ + Handles the callback from Google's OAuth. + Stores user info in session and redirects to the billing dashboard. + """ + token = await oauth.google.authorize_access_token(request) + user = token.get('userinfo') + if user: + request.session['user'] = dict(user) + return RedirectResponse(url='/static/billing-dashboard.html') + +@app.get('/auth/me') +@require_auth +async def me(request: Request): + """Returns the authenticated user's information.""" + user = request.session.get('user') + return JSONResponse(user) + +@app.get('/auth/logout') +async def logout(request: Request): + """Clears the user session and logs them out.""" + request.session.pop('user', None) + return RedirectResponse(url='/static/billing-dashboard.html') + + # Allowed emails for login ALLOWED_EMAILS = [email.strip() for email in os.environ.get("ALLOWED_EMAILS", "").split(",") if email.strip()] ALERT_EMAIL = os.environ.get("ALERT_EMAIL")