@@ -37,9 +37,9 @@ async def rerender():
37
37
38
38
39
39
@component
40
- def session_manager ():
41
- """This component can force the client (browser) to switch HTTP sessions,
42
- making it match the websocket session, by using a authentication token .
40
+ def auth_manager ():
41
+ """This component uses a client-side component alongside an authentication token
42
+ to make the client (browser) to switch the HTTP auth session, to make it match the websocket session .
43
43
44
44
Used to force persistent authentication between Django's websocket and HTTP stack."""
45
45
from reactpy_django import config
@@ -52,24 +52,24 @@ def session_manager():
52
52
def setup_asgi_scope ():
53
53
"""Store trigger functions in the websocket scope so that ReactPy-Django's hooks can command
54
54
any relevant actions."""
55
- scope ["reactpy" ]["synchronize_session " ] = synchronize_session
55
+ scope ["reactpy" ]["synchronize_auth " ] = synchronize_auth
56
56
57
57
@hooks .use_effect (dependencies = [synchronize_requested ])
58
- async def synchronize_session_watchdog ():
59
- """Detected if the client has taken too long to request a session synchronization.
58
+ async def synchronize_auth_watchdog ():
59
+ """Detected if the client has taken too long to request a auth session synchronization.
60
60
61
61
This effect will automatically be cancelled if the session is successfully
62
- switched (via effect dependencies)."""
62
+ synchronized (via effect dependencies)."""
63
63
if synchronize_requested :
64
64
await asyncio .sleep (config .REACTPY_AUTH_TOKEN_TIMEOUT + 0.1 )
65
65
await asyncio .to_thread (
66
66
_logger .warning ,
67
- f"Client did not switch sessions within { config .REACTPY_AUTH_TOKEN_TIMEOUT } (REACTPY_AUTH_TOKEN_TIMEOUT) seconds." ,
67
+ f"Client did not switch authentication sessions within { config .REACTPY_AUTH_TOKEN_TIMEOUT } (REACTPY_AUTH_TOKEN_TIMEOUT) seconds." ,
68
68
)
69
69
set_synchronize_requested (False )
70
70
71
- async def synchronize_session ():
72
- """Event that can command the client to switch HTTP sessions (to match the websocket session)."""
71
+ async def synchronize_auth ():
72
+ """Event that can command the client to switch HTTP auth sessions (to match the websocket session)."""
73
73
session : SessionBase | None = scope .get ("session" )
74
74
if not session or not session .session_key :
75
75
return
@@ -85,30 +85,31 @@ async def synchronize_session():
85
85
# Create a fresh token
86
86
token .set_current (str (uuid4 ()))
87
87
88
- # Begin the process of synchronizing HTTP and websocket sessions
88
+ # Begin the process of synchronizing HTTP and websocket auth sessions
89
89
obj = await AuthToken .objects .acreate (value = token .current , session_key = session .session_key )
90
90
await obj .asave ()
91
91
set_synchronize_requested (True )
92
92
93
- async def synchronize_session_callback (status_code : int , response : str ):
93
+ async def synchronize_auth_callback (status_code : int , response : str ):
94
94
"""This callback acts as a communication bridge, allowing the client to notify the server
95
- of the status of session switch."""
95
+ of the status of auth session switch."""
96
96
set_synchronize_requested (False )
97
97
if status_code >= 300 or status_code < 200 :
98
98
await asyncio .to_thread (
99
- _logger .warning ,
100
- f"Client returned unexpected HTTP status code ({ status_code } ) while trying to sychronize sessions." ,
99
+ _logger .error ,
100
+ f"Client returned unexpected HTTP status code ({ status_code } ) while trying to synchronize authentication sessions." ,
101
101
)
102
102
103
- # If needed, synchronize sessions by configuring all relevant session cookies.
104
- # This is achieved by commanding the client to perform a HTTP request to our session manager endpoint.
103
+ # If needed, synchronize authenication sessions by configuring all relevant session cookies.
104
+ # This is achieved by commanding the client to perform a HTTP request to our session manager endpoint,
105
+ # which will set any required cookies.
105
106
if synchronize_requested :
106
107
return HttpRequest (
107
108
{
108
109
"method" : "GET" ,
109
- "url" : reverse ("reactpy:session_manager " , args = [token .current ]),
110
+ "url" : reverse ("reactpy:auth_manager " , args = [token .current ]),
110
111
"body" : None ,
111
- "callback" : synchronize_session_callback ,
112
+ "callback" : synchronize_auth_callback ,
112
113
},
113
114
)
114
115
0 commit comments