Jon Solow
commited on
Commit
·
22491e1
1
Parent(s):
b3bd1da
Format black and fix tests
Browse files- src/login.py +10 -10
src/login.py
CHANGED
@@ -9,8 +9,9 @@ conn = st.connection("gsheets", type=GSheetsConnection)
|
|
9 |
def get_user_id_from_email_if_exists(email: str) -> int:
|
10 |
# returns -1 if id not found
|
11 |
df_email = conn.read(
|
12 |
-
|
13 |
-
|
|
|
14 |
df_email = df_email[df_email.email.notna()]
|
15 |
for row in df_email.itertuples():
|
16 |
if row.email.lower() == email.lower():
|
@@ -21,11 +22,11 @@ def get_user_id_from_email_if_exists(email: str) -> int:
|
|
21 |
def get_password_from_user_id(user_id: int) -> str | None:
|
22 |
try:
|
23 |
df_pass = conn.read(
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
except:
|
29 |
return None
|
30 |
|
31 |
|
@@ -42,8 +43,8 @@ def check_password():
|
|
42 |
def password_entered():
|
43 |
"""Checks whether a password entered by the user is correct."""
|
44 |
# check if email exists
|
45 |
-
if user_id:=get_user_id_from_email_if_exists(st.session_state["email"]):
|
46 |
-
if password:=get_password_from_user_id(user_id):
|
47 |
if hmac.compare_digest(
|
48 |
st.session_state["password"],
|
49 |
password,
|
@@ -55,7 +56,6 @@ def check_password():
|
|
55 |
return
|
56 |
st.session_state["password_correct"] = False
|
57 |
|
58 |
-
|
59 |
# Return True if the email + password is validated.
|
60 |
if st.session_state.get("password_correct", False):
|
61 |
return True
|
|
|
9 |
def get_user_id_from_email_if_exists(email: str) -> int:
|
10 |
# returns -1 if id not found
|
11 |
df_email = conn.read(
|
12 |
+
worksheet="users",
|
13 |
+
usecols=[0, 1],
|
14 |
+
)
|
15 |
df_email = df_email[df_email.email.notna()]
|
16 |
for row in df_email.itertuples():
|
17 |
if row.email.lower() == email.lower():
|
|
|
22 |
def get_password_from_user_id(user_id: int) -> str | None:
|
23 |
try:
|
24 |
df_pass = conn.read(
|
25 |
+
worksheet=f"user-{user_id}",
|
26 |
+
usecols=[0],
|
27 |
+
)
|
28 |
+
return df_pass.password[0]
|
29 |
+
except Exception:
|
30 |
return None
|
31 |
|
32 |
|
|
|
43 |
def password_entered():
|
44 |
"""Checks whether a password entered by the user is correct."""
|
45 |
# check if email exists
|
46 |
+
if user_id := get_user_id_from_email_if_exists(st.session_state["email"]):
|
47 |
+
if password := get_password_from_user_id(user_id):
|
48 |
if hmac.compare_digest(
|
49 |
st.session_state["password"],
|
50 |
password,
|
|
|
56 |
return
|
57 |
st.session_state["password_correct"] = False
|
58 |
|
|
|
59 |
# Return True if the email + password is validated.
|
60 |
if st.session_state.get("password_correct", False):
|
61 |
return True
|