Skip to content

Instantly share code, notes, and snippets.

@rbiswasfc
Created September 27, 2024 11:53
Show Gist options
  • Save rbiswasfc/0a8fced3691aabc2ef4a692f9b6b5021 to your computer and use it in GitHub Desktop.
Save rbiswasfc/0a8fced3691aabc2ef4a692f9b6b5021 to your computer and use it in GitHub Desktop.
fasthtml oauth huggingface
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