File size: 2,851 Bytes
c514c85
 
6513666
 
c514c85
6513666
 
 
 
25fb6a0
6513666
c514c85
6513666
 
 
 
 
 
 
 
 
 
c514c85
 
 
6513666
 
 
 
 
 
 
 
 
 
 
 
 
c514c85
 
 
6513666
 
 
 
 
 
 
 
 
c514c85
 
 
6513666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c514c85
 
 
6513666
 
 
 
 
 
 
 
 
 
 
 
 
 
c514c85
 
 
6513666
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# auth/schemas/auth_routes.py

from flask import Blueprint
from .auth_controller import register, login, get_user_info, update_user_info, delete_user
from flask import jsonify

# Create a Blueprint for auth-related routes
auth_bp = Blueprint('auth', __name__)


# Define the routes

@auth_bp.route('/register', methods=['POST'])
def register_user():
    """
    Route for registering a new user.
    This expects a POST request with a JSON payload containing:
    {
        "username": "user1",
        "password": "password123",
        "role": "user"  # Optional, default is "user"
    }
    Returns:
        - Success message and user ID if successful (201 Created)
        - Error message if user already exists or invalid request (400 Bad Request)
    """
    return register()


@auth_bp.route('/login', methods=['POST'])
def login_user():
    """
    Route for logging in an existing user.
    This expects a POST request with a JSON payload containing:
    {
        "username": "user1",
        "password": "password123"
    }
    Returns:
        - JWT token if login is successful (200 OK)
        - Error message if credentials are incorrect or user not found (401 Unauthorized / 404 Not Found)
    """
    return login()


@auth_bp.route('/user-info', methods=['GET'])
def get_user_details():
    """
    Route for getting the authenticated user's information.
    Requires the JWT token in the `x-access-token` header.
    Returns:
        - User information such as username, role, and creation date (200 OK)
        - Error message if the token is missing or invalid (401 Unauthorized)
    """
    return get_user_info()


@auth_bp.route('/update-user', methods=['PUT'])
def update_user():
    """
    Route for updating a user's information.
    Only accessible by managers.
    Expects a PUT request with a JSON payload containing:
    {
        "user_id": "the user's MongoDB ObjectId",
        "username": "newUsername"  # Optional
        "role": "manager"  # Optional, can be "user" or "manager"
    }
    Requires the JWT token in the `x-access-token` header.
    Returns:
        - Success message if update is successful (200 OK)
        - Error message if the user is not found or permission is denied (403 Forbidden / 404 Not Found)
    """
    return update_user_info()


@auth_bp.route('/delete-user', methods=['DELETE'])
def remove_user():
    """
    Route for deleting a user from the system.
    Only accessible by managers.
    Expects a DELETE request with a JSON payload containing:
    {
        "user_id": "the user's MongoDB ObjectId"
    }
    Requires the JWT token in the `x-access-token` header.
    Returns:
        - Success message if deletion is successful (200 OK)
        - Error message if the user is not found or permission is denied (403 Forbidden / 404 Not Found)
    """
    return delete_user()