Authentication example #82
Replies: 4 comments 1 reply
-
I also changed async def _route_function(self, request: Request):
for connect_handler in connect_handlers + ([self.connect_handler] if self.connect_handler else []):
arg_count = len(inspect.signature(connect_handler).parameters)
is_coro = is_coroutine(connect_handler)
if arg_count == 2:
await connect_handler(request, self) if is_coro else connect_handler(request, self)
elif arg_count == 1:
await connect_handler(request) if is_coro else connect_handler(request)
elif arg_count == 0:
await connect_handler() if is_coro else connect_handler()
else:
raise ValueError(f'invalid number of arguments (0, 1 or 2 allowed, got {arg_count})')
return self because I couldn't find a way to get page instance from request object. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your contribution!
As far as I know JustPy is currently migrating to Starlette's routing system: justpy-org/justpy#478 (comment) So maybe these things will get easier. 🤞 |
Beta Was this translation helpful? Give feedback.
-
In the meantime, what do you think about my approach to session data? The code keeps all session data on the server, using just session_id to get the relevant data for the session. The SessionInfo class can go to the database, to server-local files, etc. to get the needed data. |
Beta Was this translation helpful? Give feedback.
-
I'll close this Show&Tell because it refers to an outdated NiceGUI version. |
Beta Was this translation helpful? Give feedback.
-
Now that there's an idea of creating separate web pages for different users, I feel that #15 needs a working authentication example.
I tried to apply Starlette AuthenticationMiddleware to no avail. I can post what I tried if you want. I think it's due to JustPy maintaining its own routing in addition to Starlette's one?..
I made my own solution that works (please note, it includes PrivatePage class from #6):
It maintains session info on the server.
I was wondering if this approach is sound. Maybe it's better to store session data in a cookie? But I couldn't find a way to set a cookie in NiceGui.
Also since NiceGui is based on JustPy which is based on Starlette, I thought maybe there's a way to make standard Starlette AuthenticationMiddleware work?
Beta Was this translation helpful? Give feedback.
All reactions