diff --git a/oauth_example/database.py b/oauth_example/database.py index 5c3be49..6ded9c9 100644 --- a/oauth_example/database.py +++ b/oauth_example/database.py @@ -9,10 +9,11 @@ Count = user_counts.dataclass() # Auth client setup for GitHub +redirect_uri = "http://localhost:8000/auth_redirect" client = GitHubAppClient(os.getenv("AUTH_CLIENT_ID"), os.getenv("AUTH_CLIENT_SECRET"), - redirect_uri="http://localhost:8000/auth_redirect") -login_link = client.login_link() + redirect_uri=redirect_uri) +login_link = client.login_link(redirect_uri=redirect_uri) # Beforeware that puts the user_id in the request scope or redirects to the login page if not logged in. @@ -50,7 +51,7 @@ def increment(auth): @app.get('/login') def login(): return Div(P("You are not logged in."), - A('Log in with GitHub', href=client.login_link())) + A('Log in with GitHub', href=login_link)) # To log out, we just remove the user_id from the session. @app.get('/logout') @@ -66,7 +67,7 @@ def auth_redirect(code:str, session, state:str=None): print(f"state: {state}") # Not used in this example. try: # The code can be used once, to get the user info: - info = client.retr_info(code) + info = client.retr_info(code, redirect_uri=redirect_uri) print(f"info: {info}") # Use client.retr_id(code) directly if you just want the id, otherwise get the id with: user_id = info[client.id_key] @@ -90,4 +91,4 @@ def auth_redirect(code:str, session, state:str=None): print(f"Error: {e}") return f"Could not log in." -serve(port=8000) \ No newline at end of file +serve(port=8000) diff --git a/oauth_example/minimal.py b/oauth_example/minimal.py index 477ed91..3ac209d 100644 --- a/oauth_example/minimal.py +++ b/oauth_example/minimal.py @@ -3,19 +3,20 @@ import json # Auth client +redirect_uri = "http://localhost:8000/auth_redirect" client = GitHubAppClient(os.getenv("AUTH_CLIENT_ID"), os.getenv("AUTH_CLIENT_SECRET"), - redirect_uri="http://localhost:8000/auth_redirect") + redirect_uri=redirect_uri) app = FastHTML() @app.get('/') def home(): - return P("Login link: ", A("click here", href=client.login_link())) + return P("Login link: ", A("click here", href=client.login_link(redirect_uri=redirect_uri))) @app.get('/auth_redirect') def auth_redirect(code:str, session): - info = client.retr_info(code) + info = client.retr_info(code, redirect_uri=redirect_uri) return H1("Profile info"), P(json.dumps(info)) serve(port=8000) \ No newline at end of file diff --git a/oauth_example/oa.py b/oauth_example/oa.py index fe61e91..011cdee 100644 --- a/oauth_example/oa.py +++ b/oauth_example/oa.py @@ -1,7 +1,9 @@ from fasthtml.common import * from fasthtml.oauth import GoogleAppClient, OAuth -cli = GoogleAppClient.from_file('/Users/jhoward/git/nbs/oauth-test/client_secret.json') +redirect_uri = "http://localhost:5001/redirect" +cli = GoogleAppClient.from_file('/Users/jhoward/git/nbs/oauth-test/client_secret.json', + redirect_uri=redirect_uri) class Auth(OAuth): def login(self, info, state): return RedirectResponse('/', status_code=303) @@ -16,7 +18,9 @@ def chk_auth(self, info, ident, session): def home(auth): return P('Logged in!'), A('Log out', href='/logout') @app.get('/login') -def login(req): return Div(P("Not logged in"), A('Log in', href=oauth.login_link(req))) +def login(req): + return Div(P("Not logged in"), A('Log in', href=cli.login_link(redirect_uri=redirect_uri))) + serve()