I finally tried out to recover from an expired session, and it has hugely improved the user experience.
Especially for apps installed on mobile phones, as they go into sleep mode and then the old Timer trick to keep the session alive does not work.
It is simple and safe: the user is logged in again if remember=True, else the app is reloaded and they must log in.
Now the app will not reload as often, so this should be combined with a method to trigger the client side to reload when a new version is available.
import anvil.users
import anvil.server
"""
If the user signed in with remember=True, the session will be reset and the user logged in again after a session timeout.
Be aware that there may be Anvil services that will be impacted by the session reset and that you will have to handle. Please refer to the docs.
"""
def call_s(server_func_name: str, **kwargs):
return _server_call(anvil.server.call_s, server_func_name, **kwargs)
def call(server_func_name: str, **kwargs):
return _server_call(anvil.server.call, server_func_name, **kwargs)
def _server_call(server_call_func: callable, server_func_name:str, **kwargs):
try:
return server_call_func(server_func_name, **kwargs)
except anvil.server.SessionExpiredError:
anvil.server.reset_session()
if anvil.users.get_user(allow_remembered=True):
return server_call_func(server_func_name, **kwargs)
else:
raise anvil.server.SessionExpiredError()