Jon Solow
commited on
Commit
·
a27d436
1
Parent(s):
41a9958
Implement new user registration
Browse files- src/login.py +65 -18
src/login.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import hmac
|
2 |
import json
|
3 |
import pandas as pd
|
|
|
4 |
import streamlit as st
|
5 |
from streamlit_gsheets import GSheetsConnection
|
6 |
from streamlit.runtime.secrets import AttrDict, secrets_singleton
|
@@ -39,30 +40,67 @@ class HFFriendlyGSheetsConnection(GSheetsConnection):
|
|
39 |
conn = st.connection("gsheets", type=HFFriendlyGSheetsConnection)
|
40 |
|
41 |
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
"
|
47 |
-
|
48 |
-
st.text_input("Login token", type="password", key="password")
|
49 |
-
st.form_submit_button("Log in", on_click=password_entered)
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# Return True if the token is validated.
|
59 |
if st.session_state.get("password_correct", False):
|
60 |
return True
|
61 |
-
|
62 |
-
# Show inputs for email + password.
|
63 |
-
login_form()
|
64 |
-
if st.session_state.get("password_correct", True) is False:
|
65 |
-
st.error("😕 Token incorrect")
|
66 |
return False
|
67 |
|
68 |
|
@@ -100,3 +138,12 @@ def login_by_token(token: str):
|
|
100 |
st.session_state["logged_in_user"] = user_id
|
101 |
return True
|
102 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import hmac
|
2 |
import json
|
3 |
import pandas as pd
|
4 |
+
import re
|
5 |
import streamlit as st
|
6 |
from streamlit_gsheets import GSheetsConnection
|
7 |
from streamlit.runtime.secrets import AttrDict, secrets_singleton
|
|
|
40 |
conn = st.connection("gsheets", type=HFFriendlyGSheetsConnection)
|
41 |
|
42 |
|
43 |
+
# Make a regular expression
|
44 |
+
# for validating an Email
|
45 |
+
regex_email = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b"
|
46 |
+
|
47 |
+
|
48 |
+
# Define a function for
|
49 |
+
# for validating an Email
|
50 |
+
def check_email(email: str) -> bool:
|
51 |
+
return bool(re.fullmatch(regex_email, email))
|
52 |
+
|
53 |
+
|
54 |
+
def new_user_form():
|
55 |
+
"""Form to collect new user information and submit"""
|
56 |
+
with st.form("New User Registration"):
|
57 |
+
st.text_input("Email Address", key="new_user_email")
|
58 |
+
st.text_input("Name", key="new_user_name")
|
59 |
+
st.form_submit_button("Submit", on_click=new_user_submitted)
|
60 |
+
|
61 |
+
|
62 |
+
def create_new_user_request(email: str, name: str):
|
63 |
+
conn.update(
|
64 |
+
worksheet="new-user",
|
65 |
+
data=pd.DataFrame(
|
66 |
+
{
|
67 |
+
"email": email,
|
68 |
+
"name": name,
|
69 |
+
},
|
70 |
+
index=[0],
|
71 |
+
),
|
72 |
+
)
|
73 |
+
st.info("New user request submitted. Please check your email for a login url with a few minutes.")
|
74 |
+
|
75 |
+
|
76 |
+
def new_user_submitted():
|
77 |
+
if not check_email(email := st.session_state["new_user_email"]):
|
78 |
+
st.warning("Sorry email is invalid. Please try a valid email address.")
|
79 |
+
return
|
80 |
|
81 |
+
if not (name := st.session_state["new_user_name"]):
|
82 |
+
st.warning("No name entered. Please enter your name for display.")
|
83 |
+
return
|
|
|
|
|
84 |
|
85 |
+
if email_exists(email):
|
86 |
+
st.warning(
|
87 |
+
"User with that email already exists. If you would like a new login url, please click the New Login URL button."
|
88 |
+
)
|
89 |
+
return
|
90 |
+
create_new_user_request(email, name)
|
91 |
+
|
92 |
+
|
93 |
+
def check_password():
|
94 |
+
# if not logged in offer new user registration
|
95 |
+
if logged_in_user := st.session_state.get("logged_in_user"):
|
96 |
+
st.write(f"Logged in as: {logged_in_user}")
|
97 |
+
return True
|
98 |
+
else:
|
99 |
+
new_user_form()
|
100 |
|
101 |
# Return True if the token is validated.
|
102 |
if st.session_state.get("password_correct", False):
|
103 |
return True
|
|
|
|
|
|
|
|
|
|
|
104 |
return False
|
105 |
|
106 |
|
|
|
138 |
st.session_state["logged_in_user"] = user_id
|
139 |
return True
|
140 |
return False
|
141 |
+
|
142 |
+
|
143 |
+
def email_exists(email: str):
|
144 |
+
df = conn.read(
|
145 |
+
worksheet="users",
|
146 |
+
usecols=[1],
|
147 |
+
ttl=1,
|
148 |
+
)
|
149 |
+
return bool(email in df.email.tolist())
|