CoolFace
Apppublic

Irtaza328/secure_login_system

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
app.py85 linesDownload Raw Back to root
1import streamlit as st2import bcrypt3import json4import os5 6# File paths7USER_FILE = "users.json"8HISTORY_FILE = "login_history.json"9 10# Load users11if os.path.exists(USER_FILE):12    with open(USER_FILE, "r") as f:13        users = json.load(f)14else:15    users = {}16 17# Load history18if os.path.exists(HISTORY_FILE):19    with open(HISTORY_FILE, "r") as f:20        history = json.load(f)21else:22    history = []23 24# Helper functions25def save_users():26    with open(USER_FILE, "w") as f:27        json.dump(users, f)28 29def save_history():30    with open(HISTORY_FILE, "w") as f:31        json.dump(history, f)32 33def register(username, password):34    if username in users:35        return "Username already exists!"36    hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt())37    users[username] = hashed.decode()38    save_users()39    return "User registered successfully!"40 41def login(username, password):42    if username not in users:43        return "Invalid username!"44    hashed = users[username].encode()45    if bcrypt.checkpw(password.encode(), hashed):46        # Save login history47        history.append({"username": username})48        save_history()49        return "Login Successful!"50    else:51        return "Invalid password!"52 53# Streamlit App54st.title("๐Ÿ” Secure Login System")55 56menu = ["Login", "Register", "View Login History"]57choice = st.sidebar.selectbox("Menu", menu)58 59if choice == "Register":60    st.subheader("Create a new account")61    username = st.text_input("Username")62    password = st.text_input("Password", type="password")63    if st.button("Register"):64        result = register(username, password)65        st.success(result)66 67elif choice == "Login":68    st.subheader("Login to your account")69    username = st.text_input("Username", key="login_user")70    password = st.text_input("Password", type="password", key="login_pass")71    if st.button("Login"):72        result = login(username, password)73        if result == "Login Successful!":74            st.success(result)75        else:76            st.error(result)77 78elif choice == "View Login History":79    st.subheader("Login History")80    if history:81        for record in history:82            st.write(f"User: {record['username']}")83    else:84        st.write("No login records yet.")85