diff --git a/auth.py b/auth.py new file mode 100644 index 00000000..42028f9c --- /dev/null +++ b/auth.py @@ -0,0 +1,34 @@ +# auth.py +import hashlib +from db import get_users_collection + +def hash_password(password): + return hashlib.sha256(password.encode()).hexdigest() + +def register_user(username, email, password): + users_collection = get_users_collection() + if users_collection.find_one({"email": email}): + return "User already exists" + + hashed_password = hash_password(password) + user = { + "username": username, + "email": email, + "password": hashed_password + } + users_collection.insert_one(user) + return "User registered successfully" + +def login_user(email, password): + users_collection = get_users_collection() + user = users_collection.find_one({"email": email}) + + if not user: + return "Invalid credentials" + + hashed_password = hash_password(password) + if user['password'] != hashed_password: + return "Invalid credentials" + + return "Login successful" + diff --git a/db.py b/db.py new file mode 100644 index 00000000..9f0ba3ff --- /dev/null +++ b/db.py @@ -0,0 +1,15 @@ +# db.py +import pymongo +from pymongo import MongoClient + +# Replace the following with your MongoDB Atlas connection string +MONGODB_URI = "mongodb://atlas-sql-64cf1e5950ebbc37fa7eee8e-icpps.a.query.mongodb.net/ShopLocal-Assistant?ssl=true&authSource=admin" + +def get_database(): + client = MongoClient(MONGODB_URI) + return client['mongodb://atlas-sql-64cf1e5950ebbc37fa7eee8e-icpps.a.query.mongodb.net/ShopLocal-Assistant?ssl=true&authSource=admin'] + +def get_users_collection(): + db = get_database() + return db['users'] + diff --git a/streamlit_app.py b/streamlit_app.py index 4ad9e352..dd056e5c 100644 --- a/streamlit_app.py +++ b/streamlit_app.py @@ -4,6 +4,7 @@ import os import ollama from PIL import Image +from auth import register_user, login_user from ollama_api import query_ollama # Import the function # Your existing Streamlit code...