mirror of
https://github.com/gradio-app/gradio.git
synced 2025-04-06 12:30:29 +08:00
Fix the behavior of gr.LoginButton
locally and on Spaces (#9659)
* changes * add changeset * changes * add changeset * changes * add changeset * add changeset * clean * typing * button --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
parent
bad46f3e8c
commit
b1a0f6db0d
6
.changeset/khaki-lions-search.md
Normal file
6
.changeset/khaki-lions-search.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
"@gradio/button": patch
|
||||
"gradio": patch
|
||||
---
|
||||
|
||||
fix:Fix the behavior of `gr.LoginButton` locally and on Spaces
|
@ -11,6 +11,7 @@ from dataclasses import dataclass, field
|
||||
import fastapi
|
||||
from fastapi.responses import RedirectResponse
|
||||
from huggingface_hub import HfFolder, whoami
|
||||
from starlette.datastructures import URL
|
||||
|
||||
from gradio.utils import get_space
|
||||
|
||||
@ -23,8 +24,6 @@ MAX_REDIRECTS = 2
|
||||
|
||||
|
||||
def attach_oauth(app: fastapi.FastAPI):
|
||||
from gradio.route_utils import API_PREFIX
|
||||
|
||||
try:
|
||||
from starlette.middleware.sessions import SessionMiddleware
|
||||
except ImportError as e:
|
||||
@ -36,13 +35,11 @@ def attach_oauth(app: fastapi.FastAPI):
|
||||
# Add `/login/huggingface`, `/login/callback` and `/logout` routes to enable OAuth in the Gradio app.
|
||||
# If the app is running in a Space, OAuth is enabled normally. Otherwise, we mock the "real" routes to make the
|
||||
# user log in with a fake user profile - without any calls to hf.co.
|
||||
router = fastapi.APIRouter(prefix=API_PREFIX)
|
||||
app.include_router(router)
|
||||
|
||||
if get_space() is not None:
|
||||
_add_oauth_routes(router)
|
||||
_add_oauth_routes(app)
|
||||
else:
|
||||
_add_mocked_oauth_routes(router)
|
||||
_add_mocked_oauth_routes(app)
|
||||
|
||||
# Session Middleware requires a secret key to sign the cookies. Let's use a hash
|
||||
# of the OAuth secret key to make it unique to the Space + updated in case OAuth
|
||||
@ -58,7 +55,7 @@ def attach_oauth(app: fastapi.FastAPI):
|
||||
)
|
||||
|
||||
|
||||
def _add_oauth_routes(app: fastapi.APIRouter) -> None:
|
||||
def _add_oauth_routes(app: fastapi.FastAPI) -> None:
|
||||
"""Add OAuth routes to the FastAPI app (login, callback handler and logout)."""
|
||||
try:
|
||||
from authlib.integrations.base_client.errors import MismatchingStateError
|
||||
@ -157,7 +154,7 @@ def _add_oauth_routes(app: fastapi.APIRouter) -> None:
|
||||
return _redirect_to_target(request)
|
||||
|
||||
|
||||
def _add_mocked_oauth_routes(app: fastapi.APIRouter) -> None:
|
||||
def _add_mocked_oauth_routes(app: fastapi.FastAPI) -> None:
|
||||
"""Add fake oauth routes if Gradio is run locally and OAuth is enabled.
|
||||
|
||||
Clicking on a gr.LoginButton will have the same behavior as in a Space (i.e. gets redirected in a new tab) but
|
||||
@ -189,13 +186,9 @@ def _add_mocked_oauth_routes(app: fastapi.APIRouter) -> None:
|
||||
@app.get("/logout")
|
||||
async def oauth_logout(request: fastapi.Request) -> RedirectResponse:
|
||||
"""Endpoint that logs out the user (e.g. delete cookie session)."""
|
||||
from gradio.route_utils import API_PREFIX
|
||||
|
||||
request.session.pop("oauth_info", None)
|
||||
logout_url = str(request.url).replace(
|
||||
f"{API_PREFIX}/logout", "/"
|
||||
) # preserve query params
|
||||
return RedirectResponse(url=logout_url)
|
||||
logout_url = URL("/").include_query_params(**request.query_params)
|
||||
return RedirectResponse(url=logout_url, status_code=302)
|
||||
|
||||
|
||||
def _generate_redirect_uri(request: fastapi.Request) -> str:
|
||||
|
@ -190,8 +190,12 @@
|
||||
}
|
||||
|
||||
.huggingface {
|
||||
border: var(--button-border-width) solid
|
||||
var(--button-secondary-border-color);
|
||||
background: var(--background-fill-primary);
|
||||
background: rgb(20, 28, 46);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.huggingface:hover {
|
||||
background: rgb(40, 48, 66);
|
||||
color: white;
|
||||
}
|
||||
</style>
|
||||
|
Loading…
x
Reference in New Issue
Block a user