8
This commit is contained in:
@@ -31,33 +31,39 @@ def register():
|
||||
201:
|
||||
description: Пользователь успешно зарегистрирован
|
||||
"""
|
||||
data = request.json
|
||||
username = data.get('username')
|
||||
password = data.get('password')
|
||||
|
||||
if not username or not password:
|
||||
return jsonify({"error": "Username and password are required"}), 400
|
||||
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
|
||||
# Check if exists
|
||||
cur.execute("SELECT id FROM users WHERE username = %s", (username,))
|
||||
if cur.fetchone():
|
||||
try:
|
||||
data = request.json
|
||||
if not data:
|
||||
return jsonify({"error": "Invalid JSON body"}), 400
|
||||
username = data.get('username')
|
||||
password = data.get('password')
|
||||
|
||||
if not username or not password:
|
||||
return jsonify({"error": "Username and password are required"}), 400
|
||||
|
||||
conn = get_db_connection()
|
||||
cur = conn.cursor()
|
||||
|
||||
# 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)
|
||||
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)
|
||||
|
||||
return jsonify({"message": "User registered successfully", "user_id": user_id}), 201
|
||||
|
||||
return jsonify({"message": "User registered successfully", "user_id": user_id}), 201
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"error": f"Server error: {str(e)}"}), 500
|
||||
|
||||
@auth_blueprint.route('/auth/login', methods=['POST'])
|
||||
def login():
|
||||
|
||||
Reference in New Issue
Block a user