Jon Solow
commited on
Commit
·
5c97774
1
Parent(s):
1f6b17c
Get user info from the db
Browse files- src/data_storage.py +12 -1
- src/login.py +5 -19
src/data_storage.py
CHANGED
@@ -42,7 +42,7 @@ def add_new_user(email: str, name: str):
|
|
42 |
cur = con.cursor()
|
43 |
cur.execute(
|
44 |
f"""INSERT INTO users (email, name )
|
45 |
-
VALUES('{email}', '{name}')
|
46 |
"""
|
47 |
)
|
48 |
|
@@ -56,3 +56,14 @@ def get_user(user_id: int):
|
|
56 |
"email": user_data[1],
|
57 |
"name": user_data[2],
|
58 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
cur = con.cursor()
|
43 |
cur.execute(
|
44 |
f"""INSERT INTO users (email, name )
|
45 |
+
VALUES('{email.lower()}', '{name}')
|
46 |
"""
|
47 |
)
|
48 |
|
|
|
56 |
"email": user_data[1],
|
57 |
"name": user_data[2],
|
58 |
}
|
59 |
+
|
60 |
+
|
61 |
+
def get_user_id_if_email_exists(email: str) -> int | None:
|
62 |
+
with get_db_connection() as con:
|
63 |
+
cur = con.cursor()
|
64 |
+
query_result = cur.execute(f"select user_id from users where email = '{email.lower()}'").fetchone()
|
65 |
+
if query_result:
|
66 |
+
user_id = query_result[0]
|
67 |
+
else:
|
68 |
+
user_id = None
|
69 |
+
return user_id
|
src/login.py
CHANGED
@@ -5,6 +5,7 @@ import re
|
|
5 |
import streamlit as st
|
6 |
from streamlit_gsheets import GSheetsConnection
|
7 |
from streamlit.runtime.secrets import AttrDict, secrets_singleton
|
|
|
8 |
|
9 |
|
10 |
class HFFriendlyGSheetsConnection(GSheetsConnection):
|
@@ -114,7 +115,7 @@ def new_user_submitted():
|
|
114 |
st.warning("No name entered. Please enter your name for display.")
|
115 |
return
|
116 |
|
117 |
-
if
|
118 |
st.warning(
|
119 |
"User with that email already exists. If you would like a new login url, please click the New Login URL button."
|
120 |
)
|
@@ -151,27 +152,12 @@ def login_by_token(token: str):
|
|
151 |
return False
|
152 |
|
153 |
|
154 |
-
def email_exists(email: str):
|
155 |
-
df = conn.read(
|
156 |
-
worksheet="users",
|
157 |
-
usecols=[1],
|
158 |
-
ttl=1,
|
159 |
-
)
|
160 |
-
return bool(email in df.email.tolist())
|
161 |
-
|
162 |
-
|
163 |
def get_logged_in_user_name_email() -> tuple[str | None, str | None]:
|
164 |
if not (user_id := st.session_state.get("logged_in_user")):
|
165 |
# if not logged
|
166 |
return (None, None)
|
167 |
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
ttl=1,
|
172 |
-
)
|
173 |
-
|
174 |
-
user_info_series = df[df["id"] == user_id].squeeze()
|
175 |
-
email = user_info_series["email"]
|
176 |
-
name = user_info_series["name"]
|
177 |
return email, name
|
|
|
5 |
import streamlit as st
|
6 |
from streamlit_gsheets import GSheetsConnection
|
7 |
from streamlit.runtime.secrets import AttrDict, secrets_singleton
|
8 |
+
from data_storage import get_user_id_if_email_exists, get_user
|
9 |
|
10 |
|
11 |
class HFFriendlyGSheetsConnection(GSheetsConnection):
|
|
|
115 |
st.warning("No name entered. Please enter your name for display.")
|
116 |
return
|
117 |
|
118 |
+
if get_user_id_if_email_exists(email):
|
119 |
st.warning(
|
120 |
"User with that email already exists. If you would like a new login url, please click the New Login URL button."
|
121 |
)
|
|
|
152 |
return False
|
153 |
|
154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
def get_logged_in_user_name_email() -> tuple[str | None, str | None]:
|
156 |
if not (user_id := st.session_state.get("logged_in_user")):
|
157 |
# if not logged
|
158 |
return (None, None)
|
159 |
|
160 |
+
user_info_map = get_user(user_id)
|
161 |
+
email = user_info_map["email"]
|
162 |
+
name = user_info_map["name"]
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
return email, name
|