-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Richjerk
committed
May 22, 2024
1 parent
c56f132
commit 762e917
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters