-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
30 lines (24 loc) · 1.09 KB
/
Copy pathauth.py
File metadata and controls
30 lines (24 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from flask import Blueprint, request, jsonify
from flask_jwt_extended import create_access_token
from app.models import db, User
auth_bp = Blueprint('auth', __name__)
@auth_bp.route('/register', methods=['POST'])
def register():
data = request.get_json()
if not data or 'name' not in data or 'password' not in data:
return jsonify({'error': 'Faltan nombre o contraseña'}), 400
if User.query.filter_by(name=data['name']).first():
return jsonify({'error': 'El usuario ya existe'}), 409
user = User(name=data['name'])
user.set_password(data['password'])
db.session.add(user)
db.session.commit()
return jsonify({'message': 'Usuario creado', 'user_id': user.id}), 201
@auth_bp.route('/login', methods=['POST'])
def login():
data = request.get_json()
user = User.query.filter_by(name=data.get('name')).first()
if not user or not user.check_password(data.get('password', '')):
return jsonify({'error': 'Credenciales inválidas'}), 401
access_token = create_access_token(identity=str(user.id))
return jsonify({'access_token': access_token}), 200