import streamlit as st import bcrypt from db import get_database import time def create_users_collection(): db = get_database() if "users" not in db.list_collection_names(): db.create_collection("users") def signup(data): db = get_database() if db.users.find_one({"username": data["username"]}): return False, "Username already exists" elif db.users.find_one({"email": data["email"]}): return False, "Email already exists" else: hashed = bcrypt.hashpw(data["password"].encode(), bcrypt.gensalt()) data["password"] = hashed db.users.insert_one(data) return True, "Account created successfully!" def login_user(username, password): db = get_database() user = db.users.find_one({"username": username}) if user: stored_password = user["password"] if isinstance(stored_password, str): stored_password = stored_password.encode('utf-8') if bcrypt.checkpw(password.encode(), stored_password): return True, user return False, None def reset_password(email, new_password): db = get_database() user = db.users.find_one({"email": email}) if user: hashed = bcrypt.hashpw(new_password.encode(), bcrypt.gensalt()) db.users.update_one({"email": email}, {"$set": {"password": hashed}}) return True return False def show_login_page(): # Apply custom CSS for modern, animated UI st.markdown(""" """, unsafe_allow_html=True) # Hide Streamlit default elements st.markdown(""" """, unsafe_allow_html=True) # Logo and Header - Using a div container with strict spacing control st.markdown("""

TransPolymer

Get quick predictions using advanced polymer technology

""", unsafe_allow_html=True) # Initialize session state variables if "auth_mode" not in st.session_state: st.session_state.auth_mode = "login" if "loading" not in st.session_state: st.session_state.loading = False if "toast" not in st.session_state: st.session_state.toast = None # Display toast notification if needed if st.session_state.toast: toast_type, message = st.session_state.toast st.markdown(f"""
{message}
""", unsafe_allow_html=True) # Clear toast after displaying st.session_state.toast = None # Create a centered card layout col1, col2, col3 = st.columns([1, 10, 1]) with col2: st.markdown("
", unsafe_allow_html=True) # Loading spinner if st.session_state.loading: st.markdown('
', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('

Processing...

', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) time.sleep(1.5) # Simulated loading time st.session_state.loading = False st.rerun() # --- Login Form --- elif st.session_state.auth_mode == "login": st.markdown('
', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('
Username
', unsafe_allow_html=True) username = st.text_input("", key="login_username", placeholder="Enter your username") st.markdown('
', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('
Password
', unsafe_allow_html=True) password = st.text_input("", type="password", key="login_password", placeholder="Enter your password") st.markdown('
', unsafe_allow_html=True) # Login button with animation if st.button("Login Now", key="login_submit"): st.session_state.loading = True success, user = login_user(username, password) if success: st.session_state.logged_in = True st.session_state.user = user st.session_state.toast = ("success", "Login successful! Redirecting...") else: st.session_state.toast = ("error", "Invalid username or password") st.rerun() # Forgot Password Button if st.button("Forgot Password?", key="forgot_btn"): st.session_state.auth_mode = "forgot" st.rerun() # Sign Up Button if st.button("Sign Up", key="switch_to_signup"): st.session_state.auth_mode = "signup" st.rerun() st.markdown('
', unsafe_allow_html=True) # --- Sign Up Form --- elif st.session_state.auth_mode == "signup": st.markdown('
', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('
Sign up as
', unsafe_allow_html=True) role = st.selectbox("", ["Scientist", "Student", "Researcher"], key="role") st.markdown('
', unsafe_allow_html=True) col1, col2 = st.columns(2) with col1: st.markdown('
', unsafe_allow_html=True) st.markdown('
Username
', unsafe_allow_html=True) username = st.text_input("", key="signup_username", placeholder="Choose a username") st.markdown('
', unsafe_allow_html=True) with col2: st.markdown('
', unsafe_allow_html=True) st.markdown('
Email
', unsafe_allow_html=True) email = st.text_input("", key="signup_email", placeholder="Your email address") st.markdown('
', unsafe_allow_html=True) # Role-specific fields if role == "Student": col1, col2 = st.columns(2) with col1: st.markdown('
', unsafe_allow_html=True) st.markdown('
Student ID
', unsafe_allow_html=True) student_id = st.text_input("", key="signup_student_id", placeholder="Your student ID") st.markdown('
', unsafe_allow_html=True) with col2: st.markdown('
', unsafe_allow_html=True) st.markdown('
College Name
', unsafe_allow_html=True) college_name = st.text_input("", key="signup_college", placeholder="Your college name") st.markdown('
', unsafe_allow_html=True) elif role in ["Scientist", "Researcher"]: st.markdown('
', unsafe_allow_html=True) st.markdown('
Organisation
', unsafe_allow_html=True) organisation = st.text_input("", key="signup_organisation", placeholder="Your organisation name") st.markdown('
', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('
State
', unsafe_allow_html=True) state = st.text_input("", key="signup_state", placeholder="Your state") st.markdown('
', unsafe_allow_html=True) col1, col2 = st.columns(2) with col1: st.markdown('
', unsafe_allow_html=True) st.markdown('
Password
', unsafe_allow_html=True) password = st.text_input("", type="password", key="signup_password", placeholder="Create a password") st.markdown('
', unsafe_allow_html=True) with col2: st.markdown('
', unsafe_allow_html=True) st.markdown('
Confirm Password
', unsafe_allow_html=True) confirm_password = st.text_input("", type="password", key="signup_confirm", placeholder="Confirm password") st.markdown('
', unsafe_allow_html=True) # Prepare signup data if role == "Student": signup_data = { "role": role, "username": username, "student_id": student_id if 'student_id' in locals() else "", "college_name": college_name if 'college_name' in locals() else "", "email": email, "state": state, "password": password } else: signup_data = { "role": role, "username": username, "email": email, "organisation": organisation if 'organisation' in locals() else "", "state": state, "password": password } # Register button if st.button("Create Account", key="signup_submit"): if password != confirm_password: st.session_state.toast = ("error", "Passwords don't match") st.rerun() elif not username or not email or not password: st.session_state.toast = ("error", "Please fill all required fields") st.rerun() else: st.session_state.loading = True success, message = signup(signup_data) if success: st.session_state.auth_mode = "login" st.session_state.toast = ("success", message) else: st.session_state.toast = ("error", message) st.rerun() # Back to login button if st.button("Back to Login", key="back_to_login"): st.session_state.auth_mode = "login" st.rerun() st.markdown('
', unsafe_allow_html=True) # --- Forgot Password Form (only accessible from login page, but keeping code in case it's needed) --- elif st.session_state.auth_mode == "forgot": st.markdown('
', unsafe_allow_html=True) st.markdown("

Reset Password

", unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('
Email
', unsafe_allow_html=True) email = st.text_input("", key="forgot_email", placeholder="Enter your registered email") st.markdown('
', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('
New Password
', unsafe_allow_html=True) new_password = st.text_input("", type="password", key="forgot_password", placeholder="Enter new password") st.markdown('
', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('
Confirm New Password
', unsafe_allow_html=True) confirm_password = st.text_input("", type="password", key="forgot_confirm", placeholder="Confirm new password") st.markdown('
', unsafe_allow_html=True) # Reset button if st.button("Reset Password", key="reset_submit"): if not email or not new_password: st.session_state.toast = ("error", "Please fill all fields") st.rerun() elif new_password != confirm_password: st.session_state.toast = ("error", "Passwords don't match") st.rerun() else: st.session_state.loading = True if reset_password(email, new_password): st.session_state.auth_mode = "login" st.session_state.toast = ("success", "Password reset successful") else: st.session_state.toast = ("error", "Email not found") st.rerun() # Back to login button if st.button("Back to Login", key="back_btn"): st.session_state.auth_mode = "login" st.rerun() st.markdown('
', unsafe_allow_html=True) st.markdown("
", unsafe_allow_html=True)