(feat): Added PostgreSQL connection string encoding.
This commit is contained in:
parent
32dcace985
commit
51f154ab73
5 changed files with 45 additions and 32 deletions
|
|
@ -2,33 +2,18 @@ from werkzeug.security import generate_password_hash, check_password_hash
|
|||
from models.user import User, db
|
||||
|
||||
class UserService:
|
||||
def __init__(self):
|
||||
self.db = db
|
||||
|
||||
def create_user(self, username, password):
|
||||
# Check if the user exists
|
||||
existing_user = User.query.filter_by(username=username).first()
|
||||
if existing_user:
|
||||
raise ValueError("User already exists")
|
||||
|
||||
# Hash the password before storing
|
||||
hash_password = generate_password_hash(password)
|
||||
|
||||
# Create a new user
|
||||
new_user = User(username=username, password=hash_password)
|
||||
self.db.session.add(new_user)
|
||||
self.db.session.commit()
|
||||
|
||||
new_user = User(username=username, password=password)
|
||||
db.session.add(new_user)
|
||||
db.session.commit()
|
||||
return new_user
|
||||
|
||||
def verify_user(self, username, password):
|
||||
# Fetch the user by username
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if not user:
|
||||
if not user or not user.check_password(password):
|
||||
raise ValueError("Invalid username or password")
|
||||
|
||||
# Verify the hashed password
|
||||
if not check_password_hash(user.password, password):
|
||||
raise ValueError("Invalid username or password")
|
||||
|
||||
return user
|
||||
return user
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue