diff --git a/.gitignore b/.gitignore index c9f4f5a45..9fd28c6ce 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ venv .nicegui/ *.sqlite* .DS_Store +.env \ No newline at end of file diff --git a/examples/google-one-tap-auth/main.py b/examples/google-one-tap-auth/main.py new file mode 100755 index 000000000..ee86c60dc --- /dev/null +++ b/examples/google-one-tap-auth/main.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +import os + +import httpx +from dotenv import load_dotenv +from fastapi import Form, HTTPException, Request +from fastapi.responses import RedirectResponse + +from nicegui import Client, app, core, ui + +# Get your Google Client ID from the Google Cloud Console +# https://developers.google.com/identity/gsi/web/guides/get-google-api-clientid#get_your_google_api_client_id +# and pass it as an environment variable (or write it to an .env file) +load_dotenv() +GOOGLE_CLIENT_ID = os.environ.get('GOOGLE_CLIENT_ID', '') + + +@ui.page('/') +def main_page(request: Request): + assert request.url.hostname + assert core.air + assert core.air.remote_url + if 'on-air.io' not in request.url.hostname: + return RedirectResponse(core.air.remote_url) + + if not GOOGLE_CLIENT_ID: + ui.label('Please set the GOOGLE_CLIENT_ID environment variable') + return + + ui.add_head_html('') + ui.html(f''' +
+
''') + ui.label('Sign in with Google One Tap') + + +@app.post('/auth/google') +async def google_auth(request: Request, credential: str = Form(...)): + async with httpx.AsyncClient() as http_client: + response = await http_client.get(f'https://oauth2.googleapis.com/tokeninfo?id_token={credential}') + if response.status_code != 200: + raise HTTPException(status_code=400, detail='Invalid token') + user_data = response.json() + with Client(ui.page('')) as nicegui_client: + ui.label(f'Welcome {user_data["name"]}!') + return nicegui_client.build_response(request, 404) + +# NOTE we use NiceGUI On Air (https://nicegui.io/documentation/section_configuration_deployment#nicegui_on_air) +# to make the local server available to google one-tap auth +# if you deploy your app on a server, you can remove the whole On Air part +ui.run(on_air=True, storage_secret='THIS_NEEDS_TO_BE_CHANGED') diff --git a/examples/google-one-tap-auth/requirements.txt b/examples/google-one-tap-auth/requirements.txt new file mode 100644 index 000000000..40448e65c --- /dev/null +++ b/examples/google-one-tap-auth/requirements.txt @@ -0,0 +1 @@ +dotenv \ No newline at end of file