Spaces:
Sleeping
Sleeping
First full save
Browse files- app.py +82 -0
- requirements.txt +5 -0
- users.db +0 -0
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import sqlite3
|
| 3 |
+
from passlib.hash import bcrypt
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# --- Database functions ---
|
| 7 |
+
def create_usertable():
|
| 8 |
+
conn = sqlite3.connect('users.db')
|
| 9 |
+
c = conn.cursor()
|
| 10 |
+
c.execute('CREATE TABLE IF NOT EXISTS userstable(username TEXT, password TEXT)')
|
| 11 |
+
conn.commit()
|
| 12 |
+
conn.close()
|
| 13 |
+
|
| 14 |
+
def add_userdata(username, password):
|
| 15 |
+
conn = sqlite3.connect('users.db')
|
| 16 |
+
c = conn.cursor()
|
| 17 |
+
c.execute('INSERT INTO userstable(username, password) VALUES (?,?)', (username, password))
|
| 18 |
+
conn.commit()
|
| 19 |
+
conn.close()
|
| 20 |
+
|
| 21 |
+
def login_user(username, password):
|
| 22 |
+
conn = sqlite3.connect('users.db')
|
| 23 |
+
c = conn.cursor()
|
| 24 |
+
c.execute('SELECT password FROM userstable WHERE username =?', (username,))
|
| 25 |
+
stored_hash = c.fetchone()
|
| 26 |
+
conn.close()
|
| 27 |
+
|
| 28 |
+
if stored_hash:
|
| 29 |
+
stored_hash = stored_hash[0]
|
| 30 |
+
return check_hashes(password, stored_hash)
|
| 31 |
+
else:
|
| 32 |
+
return False
|
| 33 |
+
|
| 34 |
+
def view_all_users():
|
| 35 |
+
conn = sqlite3.connect('users.db')
|
| 36 |
+
c = conn.cursor()
|
| 37 |
+
c.execute('SELECT * FROM userstable')
|
| 38 |
+
data = c.fetchall()
|
| 39 |
+
conn.close()
|
| 40 |
+
return data
|
| 41 |
+
|
| 42 |
+
# --- Hashing ---
|
| 43 |
+
def make_hashes(password):
|
| 44 |
+
return bcrypt.hash(password)
|
| 45 |
+
|
| 46 |
+
def check_hashes(password, hashed_text):
|
| 47 |
+
return bcrypt.verify(password, hashed_text)
|
| 48 |
+
|
| 49 |
+
# --- Streamlit UI ---
|
| 50 |
+
st.title("User Authentication System")
|
| 51 |
+
|
| 52 |
+
create_usertable()
|
| 53 |
+
|
| 54 |
+
menu = ["Login", "SignUp", "Manage Users"]
|
| 55 |
+
choice = st.sidebar.selectbox("Menu", menu)
|
| 56 |
+
|
| 57 |
+
if choice == "Login":
|
| 58 |
+
st.subheader("Login Section")
|
| 59 |
+
username = st.text_input("User Name")
|
| 60 |
+
password = st.text_input("Password", type='password')
|
| 61 |
+
if st.button("Login"):
|
| 62 |
+
result = login_user(username.lower(), password)
|
| 63 |
+
if result:
|
| 64 |
+
st.success("Logged In as {}".format(username))
|
| 65 |
+
else:
|
| 66 |
+
st.warning("Incorrect Username/Password")
|
| 67 |
+
|
| 68 |
+
elif choice == "SignUp":
|
| 69 |
+
st.subheader("Create New Account")
|
| 70 |
+
new_user = st.text_input("Username")
|
| 71 |
+
new_password = st.text_input("Password", type='password')
|
| 72 |
+
if st.button("Signup"):
|
| 73 |
+
hashed_new_password = make_hashes(new_password)
|
| 74 |
+
add_userdata(new_user, hashed_new_password)
|
| 75 |
+
st.success("You have successfully created a valid Account")
|
| 76 |
+
st.info("Go to Login Menu to login")
|
| 77 |
+
|
| 78 |
+
elif choice == "Manage Users":
|
| 79 |
+
st.subheader("User Management")
|
| 80 |
+
user_result = view_all_users()
|
| 81 |
+
clean_db = pd.DataFrame(user_result, columns=["Username", "Password"])
|
| 82 |
+
st.dataframe(clean_db)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
streamlit-authenticator
|
| 3 |
+
passlib
|
| 4 |
+
bcrypt==3.2.0
|
| 5 |
+
pandas
|
users.db
ADDED
|
Binary file (8.19 kB). View file
|
|
|