Wauplin HF staff commited on
Commit
c28891c
1 Parent(s): 3fb96f1
Files changed (1) hide show
  1. auth.py +8 -8
auth.py CHANGED
@@ -1,12 +1,12 @@
1
- import os
2
  import hashlib
3
- from typing import Any
 
 
4
  from authlib.integrations.starlette_client import OAuth
5
  from fastapi import FastAPI
6
  from fastapi.requests import Request
7
- from fastapi.responses import RedirectResponse, HTMLResponse
8
  from starlette.middleware.sessions import SessionMiddleware
9
- from authlib.integrations.base_client import MismatchingStateError
10
 
11
 
12
  OAUTH_CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID")
@@ -31,10 +31,11 @@ oauth.register(
31
  server_metadata_url=METADATA_URL,
32
  )
33
 
34
- # Hack to close the login/logout page once the user is logged in/out.
35
- # TODO: can it be less hacky?
36
  CLOSE_WINDOW_HTML = HTMLResponse("<script>window.close();</script>")
37
- OPEN_WINDOW_HTML = HTMLResponse("<script>window.open('{url}', '_blank');</script>")
 
 
38
 
39
 
40
  async def oauth_login(request: Request):
@@ -47,7 +48,6 @@ async def oauth_login(request: Request):
47
  async def oauth_logout(request: Request) -> RedirectResponse:
48
  request.session.pop("user", None)
49
  return CLOSE_WINDOW_HTML
50
- return CLOSE_WINDOW_HTML if close_tab else RedirectResponse("/")
51
 
52
 
53
  async def oauth_redirect_callback(request: Request) -> RedirectResponse:
 
 
1
  import hashlib
2
+ import os
3
+
4
+ from authlib.integrations.base_client import MismatchingStateError
5
  from authlib.integrations.starlette_client import OAuth
6
  from fastapi import FastAPI
7
  from fastapi.requests import Request
8
+ from fastapi.responses import HTMLResponse, RedirectResponse
9
  from starlette.middleware.sessions import SessionMiddleware
 
10
 
11
 
12
  OAUTH_CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID")
 
31
  server_metadata_url=METADATA_URL,
32
  )
33
 
34
+ # Close the login/logout page once the user is logged in/out.
 
35
  CLOSE_WINDOW_HTML = HTMLResponse("<script>window.close();</script>")
36
+
37
+ # If no third-party cookie, open a new tab to login/logout + redirect to the gradio app on this tab.
38
+ OPEN_WINDOW_HTML = HTMLResponse("<script>window.open('{url}', '_blank'); window.location.replace('/');</script>")
39
 
40
 
41
  async def oauth_login(request: Request):
 
48
  async def oauth_logout(request: Request) -> RedirectResponse:
49
  request.session.pop("user", None)
50
  return CLOSE_WINDOW_HTML
 
51
 
52
 
53
  async def oauth_redirect_callback(request: Request) -> RedirectResponse: