8
This commit is contained in:
@@ -12,7 +12,13 @@ postgres_pool = pool.ThreadedConnectionPool(1, 10, dsn=DATABASE_URL)
|
|||||||
|
|
||||||
def get_db_connection():
|
def get_db_connection():
|
||||||
"""Return a connection from the pool. Caller must close it after use."""
|
"""Return a connection from the pool. Caller must close it after use."""
|
||||||
return postgres_pool.getconn()
|
conn = postgres_pool.getconn()
|
||||||
|
# Устанавливаем схему automarket (таблицы созданы там, а не в public)
|
||||||
|
cur = conn.cursor()
|
||||||
|
cur.execute("SET search_path TO automarket, public")
|
||||||
|
cur.close()
|
||||||
|
conn.commit()
|
||||||
|
return conn
|
||||||
|
|
||||||
def release_db_connection(conn):
|
def release_db_connection(conn):
|
||||||
"""Return the connection to the pool."""
|
"""Return the connection to the pool."""
|
||||||
|
|||||||
@@ -31,33 +31,39 @@ def register():
|
|||||||
201:
|
201:
|
||||||
description: Пользователь успешно зарегистрирован
|
description: Пользователь успешно зарегистрирован
|
||||||
"""
|
"""
|
||||||
data = request.json
|
try:
|
||||||
username = data.get('username')
|
data = request.json
|
||||||
password = data.get('password')
|
if not data:
|
||||||
|
return jsonify({"error": "Invalid JSON body"}), 400
|
||||||
if not username or not password:
|
username = data.get('username')
|
||||||
return jsonify({"error": "Username and password are required"}), 400
|
password = data.get('password')
|
||||||
|
|
||||||
conn = get_db_connection()
|
if not username or not password:
|
||||||
cur = conn.cursor()
|
return jsonify({"error": "Username and password are required"}), 400
|
||||||
|
|
||||||
# Check if exists
|
conn = get_db_connection()
|
||||||
cur.execute("SELECT id FROM users WHERE username = %s", (username,))
|
cur = conn.cursor()
|
||||||
if cur.fetchone():
|
|
||||||
|
# Check if exists
|
||||||
|
cur.execute("SELECT id FROM users WHERE username = %s", (username,))
|
||||||
|
if cur.fetchone():
|
||||||
|
release_db_connection(conn)
|
||||||
|
return jsonify({"error": "Username already exists"}), 400
|
||||||
|
|
||||||
|
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO users (username, password_hash) VALUES (%s, %s) RETURNING id",
|
||||||
|
(username, hashed_password)
|
||||||
|
)
|
||||||
|
user_id = cur.fetchone()[0]
|
||||||
|
conn.commit()
|
||||||
release_db_connection(conn)
|
release_db_connection(conn)
|
||||||
return jsonify({"error": "Username already exists"}), 400
|
|
||||||
|
return jsonify({"message": "User registered successfully", "user_id": user_id}), 201
|
||||||
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
|
||||||
|
except Exception as e:
|
||||||
cur.execute(
|
return jsonify({"error": f"Server error: {str(e)}"}), 500
|
||||||
"INSERT INTO users (username, password_hash) VALUES (%s, %s) RETURNING id",
|
|
||||||
(username, hashed_password)
|
|
||||||
)
|
|
||||||
user_id = cur.fetchone()[0]
|
|
||||||
conn.commit()
|
|
||||||
release_db_connection(conn)
|
|
||||||
|
|
||||||
return jsonify({"message": "User registered successfully", "user_id": user_id}), 201
|
|
||||||
|
|
||||||
@auth_blueprint.route('/auth/login', methods=['POST'])
|
@auth_blueprint.route('/auth/login', methods=['POST'])
|
||||||
def login():
|
def login():
|
||||||
|
|||||||
Reference in New Issue
Block a user