@@ -28,8 +28,7 @@ def session_manager(child: Any):
28
28
29
29
synchronize_requested , set_synchronize_requested = hooks .use_state (False )
30
30
_ , set_rerender = hooks .use_state (uuid4 )
31
- uuid_ref = hooks .use_ref (str (uuid4 ()))
32
- uuid = uuid_ref .current
31
+ uuid = hooks .use_ref ("" )
33
32
scope = hooks .use_connection ().scope
34
33
35
34
@hooks .use_effect (dependencies = [])
@@ -60,18 +59,20 @@ async def synchronize_session():
60
59
if not session or not session .session_key :
61
60
return
62
61
63
- # Delete any sessions currently associated with this UUID, which also resets
64
- # the SynchronizeSession validity time.
62
+ # Delete any sessions currently associated with the previous UUID.
65
63
# This exists to fix scenarios where...
66
- # 1) The developer manually rotates the session key.
67
- # 2) A component tree requests multiple logins back-to-back before they finish.
68
- # 3) A login is requested, but the server failed to respond to the HTTP request.
69
- with contextlib .suppress (SynchronizeSession .DoesNotExist ):
70
- obj = await SynchronizeSession .objects .aget (uuid = uuid )
71
- await obj .adelete ()
64
+ # 1) A component tree performs multiple login commands for different users.
65
+ # 2) A login is requested, but the server failed to respond to the HTTP request.
66
+ if uuid .current :
67
+ with contextlib .suppress (SynchronizeSession .DoesNotExist ):
68
+ obj = await SynchronizeSession .objects .aget (uuid = uuid .current )
69
+ await obj .adelete ()
70
+
71
+ # Create a fresh UUID
72
+ uuid .set_current (str (uuid4 ()))
72
73
73
74
# Begin the process of synchronizing HTTP and websocket sessions
74
- obj = await SynchronizeSession .objects .acreate (uuid = uuid , session_key = session .session_key )
75
+ obj = await SynchronizeSession .objects .acreate (uuid = uuid . current , session_key = session .session_key )
75
76
await obj .asave ()
76
77
set_synchronize_requested (True )
77
78
@@ -96,7 +97,7 @@ async def rerender():
96
97
http_request = HttpRequest (
97
98
{
98
99
"method" : "GET" ,
99
- "url" : reverse ("reactpy:session_manager" , args = [uuid ]),
100
+ "url" : reverse ("reactpy:session_manager" , args = [uuid . current ]),
100
101
"body" : None ,
101
102
"callback" : synchronize_session_callback ,
102
103
},
0 commit comments