Mark-Lasfar commited on
Commit
e16eacd
·
1 Parent(s): e6afcc0

Update authorize for google & github

Browse files
Files changed (1) hide show
  1. main.py +18 -12
main.py CHANGED
@@ -26,6 +26,8 @@ from hashlib import md5
26
  from datetime import datetime
27
  import re
28
  from httpx_oauth.clients.google import GoogleOAuth2
 
 
29
  # Setup logging for debugging and monitoring
30
  logging.basicConfig(level=logging.INFO)
31
  logger = logging.getLogger(__name__)
@@ -89,20 +91,10 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
89
  # CORS setup to allow requests from specific origins
90
  app.add_middleware(
91
  CORSMiddleware,
92
- #allow_origins=[
93
- #"https://mgzon-mgzon-app.hf.space", # Production domain
94
- # "http://localhost:7860", # Local development
95
- # "https://mgzon-mgzon-app.hf.space/users/me", # For user settings endpoint
96
- # Add staging domain here if needed, e.g., "https://staging.mgzon-mgzon-app.hf.space"
97
- # "http://localhost:3000",
98
- # "https://mgchat.vercel.app",
99
- # ],
100
- allow_origins=["*"],
101
  allow_credentials=True,
102
  allow_methods=["*"],
103
  allow_headers=["*"],
104
- # Optional: Uncomment to support subdomains dynamically
105
- # allow_origin_regex=r"https?://.*\.mgzon-mgzon-app\.hf\.space|http://localhost:7860",
106
  )
107
 
108
  # Include routers for authentication, user management, and API endpoints
@@ -147,6 +139,21 @@ class NotFoundMiddleware(BaseHTTPMiddleware):
147
  return templates.TemplateResponse("500.html", {"request": request, "error": str(e)}, status_code=500)
148
 
149
  app.add_middleware(NotFoundMiddleware)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  # Manual OAuth authorize endpoints (to ensure they work even if router fails)
151
  @app.get("/auth/google/authorize")
152
  async def google_authorize():
@@ -165,7 +172,6 @@ async def github_authorize():
165
  scope=["user", "user:email"],
166
  )
167
  return RedirectResponse(authorization_url)
168
-
169
 
170
  # Root endpoint for homepage
171
  @app.get("/", response_class=HTMLResponse)
 
26
  from datetime import datetime
27
  import re
28
  from httpx_oauth.clients.google import GoogleOAuth2
29
+ from httpx_oauth.exceptions import GetIdEmailError
30
+
31
  # Setup logging for debugging and monitoring
32
  logging.basicConfig(level=logging.INFO)
33
  logger = logging.getLogger(__name__)
 
91
  # CORS setup to allow requests from specific origins
92
  app.add_middleware(
93
  CORSMiddleware,
94
+ allow_origins=["*"], # Kept as wildcard for multiple projects as per request
 
 
 
 
 
 
 
 
95
  allow_credentials=True,
96
  allow_methods=["*"],
97
  allow_headers=["*"],
 
 
98
  )
99
 
100
  # Include routers for authentication, user management, and API endpoints
 
139
  return templates.TemplateResponse("500.html", {"request": request, "error": str(e)}, status_code=500)
140
 
141
  app.add_middleware(NotFoundMiddleware)
142
+
143
+ # Exception handler for OAuth errors
144
+ @app.exception_handler(GetIdEmailError)
145
+ async def handle_oauth_error(request: Request, exc: GetIdEmailError):
146
+ logger.error(f"OAuth error: {exc}")
147
+ return RedirectResponse(url="/login?error=oauth_failed", status_code=302)
148
+
149
+ # Custom Google OAuth callback to redirect to /chat
150
+ @app.get("/auth/google/callback")
151
+ async def google_oauth_callback(request: Request, user=Depends(fastapi_users.get_oauth_callback(auth_backend))):
152
+ if user:
153
+ return RedirectResponse(url="/chat", status_code=302)
154
+ else:
155
+ return RedirectResponse(url="/login?error=oauth_failed", status_code=302)
156
+
157
  # Manual OAuth authorize endpoints (to ensure they work even if router fails)
158
  @app.get("/auth/google/authorize")
159
  async def google_authorize():
 
172
  scope=["user", "user:email"],
173
  )
174
  return RedirectResponse(authorization_url)
 
175
 
176
  # Root endpoint for homepage
177
  @app.get("/", response_class=HTMLResponse)