Created
September 27, 2024 11:53
-
-
Save rbiswasfc/0a8fced3691aabc2ef4a692f9b6b5021 to your computer and use it in GitHub Desktop.
fasthtml oauth huggingface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fasthtml.common import * | |
from fasthtml.oauth import HuggingFaceClient, redir_url | |
# Auth client | |
client = HuggingFaceClient(os.getenv("HF_CLIENT_ID"), os.getenv("HF_API_SECRET")) | |
app = FastHTML() | |
redir_path = "/auth/callback" | |
@app.get("/") | |
def home(request): | |
redir = redir_url(request, redir_path) | |
link = client.login_link(redir) | |
print(f"link: {link}") | |
return P("Login link: ", A("click here", href=link)) | |
@app.get(redir_path) | |
def auth_redirect(request, code: str): | |
redir = redir_url(request, redir_path) | |
print(f"code: {code}") | |
print(f"redir: {redir}") | |
info = client.retr_info(code, redir) | |
name, username = info["name"], info["preferred_username"] | |
return (H3(f"Hello {name}!"), P(f"Your username: {username}")) | |
serve(port=8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment