When authenticating with HF OAuth, stay in same tab (#7887)

* OAuth: stay in same tab

* add changeset

* add changeset

* typo

* scroll

* add changeset

* lint

* test

* test with timeout

* log

* new test

* fix origin in postMessage

* with timeout

* shoud be fine

* lint

* lint

* remove logs

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Lucain 2024-04-10 15:15:26 +02:00 committed by GitHub
parent d28dab8224
commit 5f0248e797
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 19 additions and 16 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/app": patch
"gradio": patch
---
feat:When authenticating with HF OAuth, stay in same tab

View File

@ -14,7 +14,7 @@ from gradio.events import Events
@document("languages")
class Code(Component):
"""
Creates a code editor for viewing code (as an ouptut component), or for entering and editing code (as an input component).
Creates a code editor for viewing code (as an output component), or for entering and editing code (as an input component).
"""
languages = [

View File

@ -96,21 +96,14 @@ class LoginButton(Button):
# JS code to redirects to /login/huggingface if user is not logged in.
# If the app is opened in an iframe, open the login page in a new tab.
# Otherwise, redirects locally. Taken from https://stackoverflow.com/a/61596084.
# If user is logged in, redirect to logout page (always in-place).
# If user is logged in, redirect to /logout page. Always happens
# on the same tab.
_js_handle_redirect = """
(buttonValue) => {
if (buttonValue === BUTTON_DEFAULT_VALUE) {
url = '/login/huggingface' + window.location.search;
if ( window !== window.parent ) {
window.open(url, '_blank');
} else {
window.location.assign(url);
}
} else {
url = '/logout' + window.location.search
window.location.assign(url);
}
uri = buttonValue === BUTTON_DEFAULT_VALUE ? '/login/huggingface' : '/logout';
window.parent?.postMessage({ type: "SET_SCROLLING", enabled: true }, "*");
setTimeout(() => {
window.location.assign(uri + window.location.search);
}, 500);
}
"""

View File

@ -39,7 +39,7 @@ def attach_oauth(app: fastapi.FastAPI):
# 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
# config gets updated.
session_secret = (OAUTH_CLIENT_SECRET or "") + "-v2"
session_secret = (OAUTH_CLIENT_SECRET or "") + "-v4"
# ^ if we change the session cookie format in the future, we can bump the version of the session secret to make
# sure cookies are invalidated. Otherwise some users with an old cookie format might get a HTTP 500 error.
app.add_middleware(

View File

@ -62,6 +62,10 @@
window.iFrameResizer = {
heightCalculationMethod: "taggedElement"
};
window.parent?.postMessage(
{ type: "SET_SCROLLING", enabled: false },
"*"
);
</script>
%gradio_config%