gursi26 commited on
Commit
0078aeb
1 Parent(s): 3ba0366

working user auth

Browse files
Files changed (2) hide show
  1. app.py +26 -20
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,27 +1,33 @@
1
  import streamlit as st
2
  import uuid
 
 
 
 
 
 
 
 
 
 
3
 
4
  def get_user_id():
5
- if "user_id" not in st.session_state:
6
- # JavaScript to check if 'user_id' exists in localStorage; if not, set it
7
- st.components.v1.html(
8
- """
9
- <script>
10
- let userId = localStorage.getItem('user_id');
11
- if (!userId) {
12
- userId = '""" + str(uuid.uuid4()) + """';
13
- localStorage.setItem('user_id', userId);
14
- }
15
- window.parent.postMessage(userId, "*");
16
- </script>
17
- """,
18
- height=0,
19
- )
20
 
21
- # Capture user_id from the message
22
- st.session_state.user_id = st.query_params().get("user_id", [None])[0]
23
 
24
- return st.session_state.user_id
 
25
 
26
- user_id = get_user_id()
27
- st.write("Your user ID:", user_id)
 
1
  import streamlit as st
2
  import uuid
3
+ from streamlit_cookies_manager import CookieManager
4
+
5
+ # Initialize the cookie manager without encryption
6
+ cookies = CookieManager(
7
+ prefix="my_app_" # You can use any prefix
8
+ )
9
+
10
+ # Ensure the cookies are ready
11
+ if not cookies.ready():
12
+ st.stop()
13
 
14
  def get_user_id():
15
+ if 'user_id' not in cookies:
16
+ # Generate a new UUID
17
+ user_id = str(uuid.uuid4())
18
+ # Store it in cookies
19
+ cookies['user_id'] = user_id
20
+ cookies.save() # Save the cookies
21
+ else:
22
+ # Retrieve the user_id from cookies
23
+ user_id = cookies['user_id']
24
+ return user_id
 
 
 
 
 
25
 
26
+ def main():
27
+ st.title("Unique User ID with Persistent UUID")
28
 
29
+ user_id = get_user_id()
30
+ st.write(f"Your user ID is: `{user_id}`")
31
 
32
+ if __name__ == '__main__':
33
+ main()
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  paho-mqtt==2.1.0
2
  pillow==10.4.0
3
  setuptools==75.1.0
4
- wheel==0.44.0
 
 
1
  paho-mqtt==2.1.0
2
  pillow==10.4.0
3
  setuptools==75.1.0
4
+ wheel==0.44.0
5
+ streamlit-cookies-manager==0.2.0