-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpure_python_auth.py
More file actions
107 lines (79 loc) · 2.58 KB
/
pure_python_auth.py
File metadata and controls
107 lines (79 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import streamlit as st
import json
import os
from datetime import datetime
AUTH_FILE = "auth_data.json"
def save_auth_to_file(email, name):
auth_data = {
"email": email,
"name": name,
"timestamp": int(datetime.now().timestamp())
}
try:
with open(AUTH_FILE, 'w') as f:
json.dump(auth_data, f)
return True
except Exception as e:
st.error(f"Error saving auth data: {e}")
return False
def load_auth_from_file():
try:
if os.path.exists(AUTH_FILE):
with open(AUTH_FILE, 'r') as f:
auth_data = json.load(f)
return auth_data
except Exception as e:
st.error(f"Error loading auth data: {e}")
return None
def clear_auth_file():
try:
if os.path.exists(AUTH_FILE):
os.remove(AUTH_FILE)
return True
except Exception as e:
st.error(f"Error clearing auth data: {e}")
return False
def is_auth_valid(auth_data):
if not auth_data:
return False
current_time = int(datetime.now().timestamp())
token_time = auth_data.get("timestamp", 0)
time_diff = current_time - token_time
return time_diff < 604800
def check_and_restore_auth():
if "auth_restore_attempted" not in st.session_state:
st.session_state["auth_restore_attempted"] = True
auth_data = load_auth_from_file()
if auth_data and is_auth_valid(auth_data):
st.session_state["authenticated"] = True
st.session_state["user"] = auth_data["name"]
st.session_state["user_email"] = auth_data["email"]
return True
elif auth_data:
clear_auth_file()
return False
def login_user(email, name):
st.session_state["authenticated"] = True
st.session_state["user"] = name
st.session_state["user_email"] = email
st.session_state["reset_sidebar_on_login"] = True # Flag to reset sidebar state
save_auth_to_file(email, name)
return True
def logout_user():
st.session_state["authenticated"] = False
st.session_state["user"] = None
st.session_state["user_email"] = None
st.session_state["force_sidebar_collapse"] = True
clear_auth_file()
return True
def is_authenticated():
return st.session_state.get("authenticated", False)
def get_current_user():
if is_authenticated():
return {
"name": st.session_state.get("user"),
"email": st.session_state.get("user_email")
}
return None
def init_persistent_auth():
check_and_restore_auth()