|
9 | 9 | from nicegui import app, ui
|
10 | 10 |
|
11 | 11 | # Get your Google Client ID from the Google Cloud Console and pass it as an environment variable (or write it to an .env file)
|
12 |
| -# For local development, you should add http://localhost:8080 to the authorized javascript origins |
13 |
| -# In production, you should add the domain of your website to the authorized javascript origins |
14 |
| -# see https://developers.google.com/identity/gsi/web/guides/get-google-api-clientid#get_your_google_api_client_id |
| 12 | +# For local development, you should add http://localhost:8080 to the authorized JavaScript origins. |
| 13 | +# In production, you should add the domain of your website to the authorized JavaScript origins. |
| 14 | +# See https://developers.google.com/identity/gsi/web/guides/get-google-api-clientid#get_your_google_api_client_id. |
15 | 15 | load_dotenv()
|
16 | 16 | GOOGLE_CLIENT_ID = os.environ.get('GOOGLE_CLIENT_ID', '')
|
17 | 17 |
|
18 | 18 |
|
19 | 19 | @ui.page('/')
|
20 |
| -def main_page(): |
| 20 | +def main_page() -> None: |
21 | 21 | user_data = app.storage.user.get('user_data', None)
|
22 | 22 | if not user_data:
|
23 | 23 | ui.add_head_html('<script src="https://accounts.google.com/gsi/client" async defer></script>')
|
24 | 24 | ui.html(f'''
|
25 | 25 | <div id="g_id_onload"
|
26 | 26 | data-client_id="{GOOGLE_CLIENT_ID}"
|
27 | 27 | data-login_uri="http://localhost:8080/auth">
|
28 |
| - </div>''') |
| 28 | + </div> |
| 29 | + ''') |
29 | 30 | ui.label('Sign in with Google One Tap')
|
30 | 31 | else:
|
31 | 32 | ui.label(f'Welcome {user_data["name"]}!')
|
32 | 33 | ui.button('Logout', on_click=logout)
|
33 | 34 |
|
34 | 35 |
|
35 |
| -def logout(): |
| 36 | +def logout() -> None: |
36 | 37 | del app.storage.user['user_data']
|
37 | 38 | ui.navigate.to('/')
|
38 | 39 |
|
39 | 40 |
|
40 | 41 | @app.post('/auth')
|
41 |
| -async def google_auth(credential: str = Form(...)): |
| 42 | +async def google_auth(credential: str = Form(...)) -> RedirectResponse: |
42 | 43 | async with httpx.AsyncClient() as http_client:
|
43 | 44 | response = await http_client.get(f'https://oauth2.googleapis.com/tokeninfo?id_token={credential}')
|
44 | 45 | if response.status_code != 200:
|
|
0 commit comments