forked from SANTHAN-KUMAR/Answer-sheet-processor
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth.py
More file actions
45 lines (40 loc) · 1.32 KB
/
auth.py
File metadata and controls
45 lines (40 loc) · 1.32 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
# auth.py
import streamlit as st
import hashlib
from db import get_user_collection
from utils import generate_otp, send_otp_email
def hash_password(password):
return hashlib.sha256((password + st.secrets["HASH_SECRET_KEY"]).encode()).hexdigest()
def signup(username, email, password):
users = get_user_collection()
if users.find_one({"email": email}):
return "Email already exists."
otp = generate_otp()
send_otp_email(email, otp)
st.session_state.temp_signup = {
"username": username,
"email": email,
"password": hash_password(password),
"otp": otp
}
return "OTP sent to your email."
def verify_otp(entered_otp):
data = st.session_state.get("temp_signup", {})
if not data:
return "Session expired. Please sign up again."
if data["otp"] == entered_otp:
users = get_user_collection()
users.insert_one({
"username": data["username"],
"email": data["email"],
"password": data["password"]
})
del st.session_state.temp_signup
return "Account created!"
return "Incorrect OTP."
def login(email, password):
users = get_user_collection()
user = users.find_one({"email": email})
if not user or user["password"] != hash_password(password):
return None
return user