Team Ai
Apppublic

saxon7/Coder_Coding

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
home.py58 linesDownload Raw Back to root
1import streamlit as st2import hashlib3 4def make_hashes(password):5  """Hashes a password using SHA-256."""6  return hashlib.sha256(str.encode(password)).hexdigest()7 8def check_hashes(password,hashed_text):9  """Checks if a hashed password matches the stored hash."""10  if make_hashes(password) == hashed_text:11    return hashed_text12  return False13 14# Sample username and hashed password (in real-world scenarios, store these securely)15users = {16    "user1": make_hashes("password1"), 17    "user2": make_hashes("password2") 18}19 20def main():21  """Main function for the Streamlit app."""22  st.title("Login/Logout Page")23 24  menu = ["Login", "SignUp"]25  choice = st.sidebar.selectbox("Menu", menu)26 27  if choice == "Login":28    st.subheader("Login Section")29    username = st.text_input("User Name")30    password = st.text_input("Password", type='password')31    if st.button("Login"):32      if username in users and check_hashes(password, users[username]):33        st.success("Logged In as {}".format(username))34        # Session state to track login status35        st.session_state.logged_in = True 36      else:37        st.warning("Incorrect Username/Password")38 39  elif choice == "SignUp":40    st.subheader("Create New Account")41    new_user = st.text_input("Username")42    new_password = st.text_input("Password", type='password')43    if st.button("Signup"):44      if new_user in users:45        st.warning("Username already exists")46      else:47        users[new_user] = make_hashes(new_password)  # Update user data48        st.success("You have successfully created an account")49 50  # Logout button (visible only when logged in)51  if st.session_state.get('logged_in', False): 52    if st.sidebar.button("Logout"):53      st.session_state.logged_in = False54      st.success("You have successfully logged out")55      st.experimental_rerun()  # Refresh the page56 57if __name__ == '__main__':58  main()