File size: 1,431 Bytes
afd0ae6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from flask import Blueprint, request, jsonify
from api.services.auth_service import AuthService
from flask_jwt_extended import jwt_required, get_jwt_identity
from api.models.user import User

auth_bp = Blueprint('auth', __name__, url_prefix='/api/auth')

@auth_bp.route('/login', methods=['POST'])
def login():
    data = request.get_json()
    email = data.get('email')
    password = data.get('password')
    
    if not email or not password:
        return jsonify({'status': False, 'message': 'Missing email or password'}), 400
        
    result = AuthService.login(email, password)
    if result['status']:
        return jsonify(result), 200
    return jsonify(result), 401

@auth_bp.route('/register', methods=['POST'])
def register():
    data = request.get_json()
    email = data.get('email')
    password = data.get('password')
    name = data.get('name')
    
    if not email or not password or not name:
        return jsonify({'status': False, 'message': 'Missing required fields'}), 400
        
    result = AuthService.register(email, password, name)
    if result['status']:
        return jsonify(result), 201
    return jsonify(result), 400

@auth_bp.route('/me', methods=['GET'])
@jwt_required()
def get_me():
    user_id = get_jwt_identity()
    user = User.query.get(user_id)
    if user:
        return jsonify(user.to_dict()), 200
    return jsonify({'status': False, 'message': 'User not found'}), 404