feat: add login endpoint
This commit is contained in:
@@ -1,12 +1,24 @@
|
||||
from db.database import PostgresConnector
|
||||
from flask_bcrypt import Bcrypt
|
||||
|
||||
class AuthManager:
|
||||
def __init__(self, db: PostgresConnector, bcrypt):
|
||||
def __init__(self, db: PostgresConnector, bcrypt: Bcrypt):
|
||||
self.db = db
|
||||
self.bcrypt = bcrypt
|
||||
|
||||
def register_user(self, username, password):
|
||||
# Hash the password
|
||||
def register_user(self, username, email, password):
|
||||
hashed_password = self.bcrypt.generate_password_hash(password).decode("utf-8")
|
||||
# Save the user to the database
|
||||
self.db.save_user(username, hashed_password)
|
||||
|
||||
if self.db.get_user_by_email(email):
|
||||
raise ValueError("Email already registered")
|
||||
|
||||
if self.db.get_user_by_username(username):
|
||||
raise ValueError("Username already taken")
|
||||
|
||||
self.db.save_user(username, email, hashed_password)
|
||||
|
||||
def authenticate_user(self, username, password):
|
||||
user = self.db.get_user_by_username(username)
|
||||
if user and self.bcrypt.check_password_hash(user['password_hash'], password):
|
||||
return user
|
||||
return None
|
||||
Reference in New Issue
Block a user