transpolymer commited on
Commit
8214463
·
verified ·
1 Parent(s): 4650743

Upload login_page.py

Browse files
Files changed (1) hide show
  1. login_page.py +143 -0
login_page.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import bcrypt
3
+ from db import get_database
4
+
5
+ def create_users_collection():
6
+ db = get_database()
7
+ if "users" not in db.list_collection_names():
8
+ db.create_collection("users")
9
+
10
+ def signup(username, first_name, last_name, email, password):
11
+ db = get_database()
12
+ if db.users.find_one({"username": username}):
13
+ st.error("Username already exists")
14
+ else:
15
+ hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
16
+ db.users.insert_one({
17
+ "username": username,
18
+ "first_name": first_name,
19
+ "last_name": last_name,
20
+ "email": email,
21
+ "password": hashed
22
+ })
23
+ st.success("Account created. Please log in.")
24
+
25
+ def login_user(username, password):
26
+ db = get_database()
27
+ user = db.users.find_one({"username": username})
28
+ if user:
29
+ stored_password = user["password"]
30
+ if isinstance(stored_password, str):
31
+ stored_password = stored_password.encode('utf-8')
32
+ if bcrypt.checkpw(password.encode(), stored_password):
33
+ return True
34
+ return False
35
+
36
+ def reset_password(email, new_password):
37
+ db = get_database()
38
+ user = db.users.find_one({"email": email})
39
+ if user:
40
+ hashed = bcrypt.hashpw(new_password.encode(), bcrypt.gensalt())
41
+ db.users.update_one({"email": email}, {"$set": {"password": hashed}})
42
+ return True
43
+ return False
44
+
45
+ def show_login_page():
46
+ # Custom CSS
47
+ st.markdown("""
48
+ <style>
49
+ .main .block-container {
50
+ padding-top: 1rem !important;
51
+ padding-bottom: 1rem !important;
52
+ }
53
+ .login-card {
54
+ background-color: #f0f2f6;
55
+ border-radius: 12px;
56
+ padding: 25px 30px;
57
+ box-shadow: 0 4px 8px rgba(0,0,0,0.1);
58
+ }
59
+ .centered-heading {
60
+ text-align: center;
61
+ color: #4B8BBE;
62
+ }
63
+ .subheading {
64
+ text-align: center;
65
+ color: gray;
66
+ }
67
+ .stButton > button {
68
+ width: 100%;
69
+ padding: 6px 0;
70
+ font-size: 14px;
71
+ background-color: #4B8BBE;
72
+ color: white;
73
+ border: none;
74
+ border-radius: 5px;
75
+ }
76
+ </style>
77
+ """, unsafe_allow_html=True)
78
+
79
+ # Title and subtitle
80
+ st.markdown("<h1 class='centered-heading'>TransPolymer</h1>", unsafe_allow_html=True)
81
+ st.markdown("<h4 class='subheading'>Get quick predictions using TransPolymer</h4>", unsafe_allow_html=True)
82
+
83
+ # Centered form
84
+ col1, col2, col3 = st.columns([1, 2, 1])
85
+ with col2:
86
+ st.markdown("<div class='login-card'>", unsafe_allow_html=True)
87
+
88
+ # Auth mode toggle
89
+ col_login, col_signup = st.columns(2)
90
+ with col_login:
91
+ if st.button("Login", key="login_btn"):
92
+ st.session_state.auth_mode = "login"
93
+ with col_signup:
94
+ if st.button("Sign Up", key="signup_btn"):
95
+ st.session_state.auth_mode = "signup"
96
+
97
+ # Set default mode
98
+ if "auth_mode" not in st.session_state:
99
+ st.session_state.auth_mode = "login"
100
+
101
+ st.markdown("<br>", unsafe_allow_html=True)
102
+
103
+ # --- Login Mode ---
104
+ if st.session_state.auth_mode == "login":
105
+ username = st.text_input("Username", key="login_username")
106
+ password = st.text_input("Password", type="password", key="login_password")
107
+ if st.button("Login Now"):
108
+ if login_user(username, password):
109
+ st.session_state.logged_in = True
110
+ st.success("Login Successful")
111
+ st.rerun()
112
+ else:
113
+ st.error("Invalid username or password")
114
+ if st.button("Forgot Password?"):
115
+ st.session_state.auth_mode = "forgot"
116
+ st.rerun()
117
+
118
+ # --- Signup Mode ---
119
+ elif st.session_state.auth_mode == "signup":
120
+ username = st.text_input("New Username", key="signup_username")
121
+ first_name = st.text_input("First Name", key="signup_first_name")
122
+ last_name = st.text_input("Last Name", key="signup_last_name")
123
+ email = st.text_input("Email", key="signup_email")
124
+ password = st.text_input("Password", type="password", key="signup_password")
125
+
126
+ if st.button("Create Account"):
127
+ signup(username, first_name, last_name, email, password)
128
+ st.session_state.auth_mode = "login"
129
+ st.rerun()
130
+
131
+ # --- Forgot Password Mode ---
132
+ elif st.session_state.auth_mode == "forgot":
133
+ email = st.text_input("Enter registered email", key="forgot_email")
134
+ new_password = st.text_input("New password", type="password", key="forgot_password")
135
+ if st.button("Reset Password"):
136
+ if reset_password(email, new_password):
137
+ st.success("Password reset successful. Please login.")
138
+ st.session_state.auth_mode = "login"
139
+ st.rerun()
140
+ else:
141
+ st.error("Email not found.")
142
+
143
+ st.markdown("</div>", unsafe_allow_html=True)