flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
9.43k stars 360 forks source link

Authentication does not work in Desktop mode (macos) #3168

Closed dkzkv closed 2 weeks ago

dkzkv commented 2 weeks ago

✅ Checked Issues

✅ Checked Stackoverflow

ℹ️ Description

Authentication in desktop mode does not work.

🤖 Code example to reproduce the issue:

import flet as ft
from urllib.parse import urlparse

from flet_runtime.auth.providers import GoogleOAuthProvider

GOOGLE_CLIENT_ID = "{YOUR ID HERE}"
GOOGLE_CLIENT_SECRET = "{YOUR SECRET HERE}"

async def main(page: ft.Page):
    google_provider = GoogleOAuthProvider(
        client_id=GOOGLE_CLIENT_ID,
        client_secret=GOOGLE_CLIENT_SECRET,
        redirect_url=f"http://localhost:{urlparse(page.url).port}/oauth_callback",
    )

    info_text = ft.TextField("", multiline=True)

    async def google_logon(e):
        await page.login_async(google_provider)

    async def on_login(e):
        print("Login error:", e.error)
        print("Access token:", page.auth.token.access_token)
        print("User ID:", page.auth.user.id)
        info_text.value = page.auth.user.id
        await info_text.update_async()

    page.on_login = on_login
    page.add(ft.ElevatedButton("Continue with Google", on_click=google_logon), info_text)

ft.app(target=main)

⚠️ Actual result:

When calling the function await page.login_async(google_provider), I get the following error:

Exception: oauthAuthorize command is not supported.

🥳 Expected result:

The function should open a window with google authentication in the browser (docs)

⭐️ Additional information:

Attempt to fix 1

if i add ft.FLET_APP_WEB:

ft.app(target=main, view=ft.FLET_APP_WEB)

Then the authentication works, it redirects me to the browser and after authentication redirects me back to the application. BUT: but it only works if I run the application from the IDE. If I try to build an application 'flet build macos' and then authenticate...i get same error Exception: oauthAuthorize command is not supported..

Attempt to fix 2

Building flet build macos with

ft.app(target=main, port=8550, view=ft.WEB_BROWSER)

this leads to the fact that after the start of the application, an empty screen is displayed and the controllers are not loaded (apparently some kind of internal logic)

Flet version::

flet=0.22.0 Python version 3.11.6 Operating system:

Macos M2 Sonoma 14.4.1 Build command: flet build macos

FeodorFitsner commented 2 weeks ago

It's actually "as designed". Using OAuth doesn't make sense with a standalone desktop app as secret key comes with the app and any user could retrieve it.

What could work though is Flet desktop app that works with a remote web server keeping secret keys and controlling auth process.

What is your deployment scenario here?