Skip to content

Commit

Permalink
🐛 fix: error when using the 'login()' method in async function (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daxexs committed Dec 30, 2024
1 parent b05b856 commit 96ccca6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
75 changes: 59 additions & 16 deletions src/flet_easy/datasy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from flet import Page

from flet_easy.exceptions import LoginError
from flet_easy.extra import Msg, Redirect
from flet_easy.extrasJwt import (
SecretKey,
Expand Down Expand Up @@ -275,24 +276,14 @@ def _create_tasks(self, time_expiry: timedelta, key: str, sleep: int) -> None:
sleep_time=sleep,
).start()

def login(
def __login(
self,
key: str,
value: Dict[str, Any] | Any,
next_route: str,
time_expiry: timedelta = None,
sleep: int = 1,
):
"""Registering in the client's storage the key and value in all browser sessions.
### Parameters to use:
* `key` : It is the identifier to store the value in the client storage.
* `value` : Recommend to use a dict if you use JWT.
* `next_route` : Redirect to next route after creating login.
* `time_expiry` : Time to expire the session, use the `timedelta` class to configure. (Optional)
* `sleep` : Time to do login checks, default is 1s. (Optional)
"""
) -> str | None:
if time_expiry:
assert isinstance(
value, Dict
Expand All @@ -308,18 +299,70 @@ def login(
value = encode_verified(self.secret_key, value, time_expiry)
self._login_done = True

if self.__auto_logout:
self._create_tasks(time_expiry, key, sleep)

self.page.run_task(self.page.client_storage.set_async, key, value).result()
if self.__auto_logout:
self._create_tasks(time_expiry, key, sleep)

if self.page.web:
self.page.pubsub.send_others_on_topic(
self.page.client_ip + self.page.client_user_agent,
Msg("login", key, {"value": value, "next_route": next_route}),
)

return value

def login(
self,
key: str,
value: Dict[str, Any] | Any,
next_route: str,
time_expiry: timedelta = None,
sleep: int = 1,
):
"""Registering in the client's storage the key and value in all browser sessions.
### Parameters to use:
* `key` : It is the identifier to store the value in the client storage.
* `value` : Recommend to use a dict if you use JWT.
* `next_route` : Redirect to next route after creating login.
* `time_expiry` : Time to expire the session, use the `timedelta` class to configure. (Optional)
* `sleep` : Time to do login checks, default is 1s. (Optional)
"""
value = self.__login(key, value, next_route, time_expiry, sleep)

try:
self.page.client_storage.set(key, value)
except TimeoutError:
raise LoginError(
"The operation has timed out. Please use 'login_async()' instead of 'login()'."
)

self.__go(next_route)

async def login_async(
self,
key: str,
value: Dict[str, Any] | Any,
next_route: str,
time_expiry: timedelta = None,
sleep: int = 1,
):
"""Registering in the client's storage the key and value in all browser sessions.
* This method is asynchronous.
### Parameters to use:
* `key` : It is the identifier to store the value in the client storage.
* `value` : Recommend to use a dict if you use JWT.
* `next_route` : Redirect to next route after creating login.
* `time_expiry` : Time to expire the session, use the `timedelta` class to configure. (Optional)
* `sleep` : Time to do login checks, default is 1s. (Optional)
"""

value = self.__login(key, value, next_route, time_expiry, sleep)
await self.page.client_storage.set_async(key, value)
self.page.run_thread(self.__go, next_route)

""" Page go """

def go(self, route: str):
Expand Down
10 changes: 10 additions & 0 deletions src/flet_easy/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class FletEasyError(Exception):
"""FletEasy error captured"""

pass


class LoginError(FletEasyError):
"""Login error captured"""

pass

0 comments on commit 96ccca6

Please sign in to comment.