-
Notifications
You must be signed in to change notification settings - Fork 0
/
Homepage.py
293 lines (235 loc) · 8.76 KB
/
Homepage.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import random
import re
import smtplib
from email.mime.text import MIMEText
from time import sleep
import psycopg2
import streamlit as st
from argon2 import PasswordHasher
from navigation import make_sidebar
ph = PasswordHasher()
# Function to validate email format
def validate_email(email):
"""
Validates the format of an email address.
Args:
email (str): The email address to be validated.
Returns:
bool: True if the email address is valid, False otherwise.
"""
return bool(re.match(r"[^@]+@[^@]+\.[^@]+", email))
# Function to validate password strength
def validate_password(password):
"""
Validates the strength of a password.
Args:
password (str): The password to be validated.
Returns:
bool: True if the password is at least 8 characters long, False otherwise.
"""
return len(password) >= 8
# Hash a password
def hash_password(password):
"""
Hashes a given password using the PasswordHasher instance.
Args:
password (str): The password to be hashed.
Returns:
str: The hashed password.
"""
return ph.hash(password)
# Check password
def check_password(stored_password, provided_password):
"""
Checks if the provided password matches the stored password.
Args:
stored_password (str): The password stored in the system.
provided_password (str): The password provided by the user.
Returns:
bool: True if the passwords match, False otherwise.
"""
try:
ph.verify(stored_password, provided_password)
return True
except ValueError:
return False
# Function to handle user sign-up
def signup(email, password):
"""
Handles the user sign-up process.
Args:
email (str): The user's email address.
password (str): The user's password.
Returns:
tuple: A boolean indicating whether the sign-up was successful, and a message describing the result.
"""
if not validate_email(email):
return False, "Invalid email format."
elif not validate_password(password):
return False, "Password should be at least 8 characters long."
connection = psycopg2.connect(
dbname=st.secrets["database"]["dbname"],
user=st.secrets["database"]["user"],
password=st.secrets["database"]["password"],
host=st.secrets["database"]["host"],
port=st.secrets["database"]["port"],
)
cursor = connection.cursor()
cursor.execute("SELECT * FROM users WHERE email = %s", (email,))
if cursor.fetchone() is not None:
cursor.close()
connection.close()
return False, "User already exists."
# Generate and send verification code
verification_code = generate_verification_code()
if not send_verification_email(email, verification_code):
return False, "Failed to send verification email."
# Store the user data temporarily
if "signup_email" not in st.session_state:
st.session_state["signup_email"] = email
st.session_state["signup_password"] = password
st.session_state["verification_code"] = verification_code
return True, "Verification code sent to your email."
# Function to handle user sign-in
def signin(email, password):
"""
Handles the user sign-in process.
Args:
email (str): The user's email address.
password (str): The user's password.
Returns:
tuple: A boolean indicating whether the sign-in was successful, and a message describing the result.
"""
connection = psycopg2.connect(
dbname=st.secrets["database"]["dbname"],
user=st.secrets["database"]["user"],
password=st.secrets["database"]["password"],
host=st.secrets["database"]["host"],
port=st.secrets["database"]["port"],
)
cursor = connection.cursor()
cursor.execute("SELECT password FROM users WHERE email = %s", (email,))
user = cursor.fetchone()
cursor.close()
connection.close()
if user is None:
return False, "User does not exist."
elif not check_password(user[0], password):
return False, "Incorrect password."
else:
return True, "User signed in successfully."
def send_verification_email(email, code):
"""
Sends a verification email to the specified email address.
Args:
email (str): The recipient's email address.
code (str): The verification code to be sent.
Returns:
bool: True if the email is sent successfully, False otherwise.
"""
sender_email = st.secrets["owner"]["email"]
sender_password = st.secrets["owner"]["password"]
subject = "Your Verification Code for DailyLinkai"
body = f"Your code is: {code}."
msg = MIMEText(body)
msg["Subject"] = subject
msg["From"] = sender_email
msg["To"] = email
try:
smtp_server, smtp_port = "smtp.gmail.com", 587
server = smtplib.SMTP(smtp_server, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()
server.login(sender_email, sender_password)
server.sendmail(sender_email, [email], msg.as_string())
server.quit()
return True
except ValueError as ve:
print(f"Error: {ve}")
return False
except Exception as e:
print(f"Error sending email: {e}")
return False
# Function to generate verification code
def generate_verification_code():
"""
Generates a random verification code.
Returns:
str: A 6-digit verification code as a string.
"""
return str(random.randint(100000, 999999))
make_sidebar()
st.title("✨ DailyLinkai ✨")
# Ensure `signup_step` is initialized
if "signup_step" not in st.session_state:
st.session_state["signup_step"] = 1
with st.expander("Sign In", expanded=False):
st.subheader("Sign In to your account")
# Sign in form
signin_email = st.text_input("Email", key="signin_email")
signin_password = st.text_input("Password", type="password", key="signin_password")
if st.button("Sign In"):
success, message = signin(signin_email, signin_password)
if success:
st.success(message)
st.session_state.logged_in = True
st.session_state["user_email"] = (
signin_email # Use a different key to avoid the exception
)
# st.success("Logged in successfully!")
sleep(0.5)
st.switch_page("pages/app.py")
else:
st.error(message)
if st.session_state["signup_step"] == 1:
with st.expander("Sign Up", expanded=False):
st.subheader("Create a new account")
signup_email = st.text_input("Email", key="signup_email")
signup_password = st.text_input(
"Password", type="password", key="signup_password"
)
if st.button("Sign Up"):
if not validate_email(signup_email):
st.error("Invalid email format.")
elif not validate_password(signup_password):
st.error("Password should be at least 8 characters long.")
else:
success, message = signup(signup_email, signup_password)
if success:
st.success(message)
st.session_state["signup_step"] = 2
else:
st.error(message)
if st.session_state["signup_step"] == 2:
st.subheader("Verify Email")
verification_code = st.text_input("Verification Code", key="verification")
if st.button("Verify"):
if verification_code == st.session_state["verification_code"]:
hashed_password = hash_password(st.session_state["signup_password"])
connection = psycopg2.connect(
dbname=st.secrets["database"]["dbname"],
user=st.secrets["database"]["user"],
password=st.secrets["database"]["password"],
host=st.secrets["database"]["host"],
port=st.secrets["database"]["port"],
)
cursor = connection.cursor()
cursor.execute(
"INSERT INTO users (email, password) VALUES (%s, %s)",
(st.session_state["signup_email"], hashed_password),
)
connection.commit()
cursor.close()
connection.close()
st.success("Account created and logged in successfully!")
st.session_state.logged_in = True
st.session_state["user_email"] = st.session_state["signup_email"]
st.session_state["signup_step"] = 1
st.session_state.pop("signup_email", None)
st.session_state.pop("signup_password", None)
st.session_state.pop("verification_code", None)
sleep(0.5)
st.switch_page("pages/app.py")
else:
st.error("Incorrect verification code.")