Event-CRUD-Flask-python3-API/routes/userRoutes.py

84 lines
2.9 KiB
Python
Raw Permalink Normal View History

2024-01-09 09:22:53 +00:00
from flask import Blueprint, jsonify, request, g
2024-01-07 11:28:49 +00:00
from services.UserService import UserService
2024-01-07 14:30:55 +00:00
from flask_jwt_extended import JWTManager, jwt_required, create_access_token
2024-01-09 09:22:53 +00:00
from middlewares.userMiddlewares import validate_user_post_request, validate_user_login_request, authenticate_user
2024-01-07 11:28:49 +00:00
userRoutes = Blueprint('userRoutes', __name__)
@userRoutes.route('/', methods=['GET'])
@userRoutes.route('', methods=['GET'])
def allUsers():
users = UserService.get_all_users()
return jsonify(users), 200
2024-01-09 09:17:01 +00:00
@userRoutes.route('/<string:user_id>', methods=['GET'])
def getUser(user_id):
user = UserService.get_user_by_id(user_id)
if user:
return jsonify(user), 200
else:
return jsonify({'error': 'User not found'}), 404
2024-01-09 09:22:53 +00:00
@userRoutes.route('/<string:user_id>', methods=['PUT'])
@validate_user_post_request
@authenticate_user
def updateUser(user_id):
try:
if user_id != g.user_id:
return jsonify({'error': 'Unauthorized'}), 401
data = request.json
updated_user = UserService.update_user(user_id, data)
if updated_user:
return jsonify(updated_user), 200
else:
return jsonify({'error': 'User not found'}), 404
except Exception as e:
return jsonify({'error': str(e)}), 500
2024-01-09 09:17:01 +00:00
2024-01-07 11:28:49 +00:00
@userRoutes.route('/', methods=['POST'])
@userRoutes.route('', methods=['POST'])
@validate_user_post_request
def createNewUser():
try:
data = request.json
if UserService.get_user_by_email(data['email']):
return jsonify({'error': 'User already exists'}), 400
new_user = UserService.create_user(data)
if new_user:
return jsonify(new_user), 201
else:
return jsonify({'error': 'Failed to create user'}), 400
except Exception as e:
return jsonify({'error': str(e)}), 500
@userRoutes.route('/login', methods=['POST'])
@validate_user_login_request
def loginUser():
try:
data = request.json
user = UserService.verify_user(data)
if user:
2024-01-09 08:14:28 +00:00
response = login(user) # Get the response from the login function
2024-01-07 11:28:49 +00:00
return response, 200
else:
return jsonify({'error': 'Invalid credentials'}), 400
except Exception as e:
return jsonify({'error': str(e)}), 500
2024-01-09 08:14:28 +00:00
2024-01-07 11:42:48 +00:00
@userRoutes.route('/logout', methods=['POST'])
@jwt_required(optional=True)
def logoutUser():
try:
response = jsonify({'message': 'Logged out successfully'})
response.set_cookie('access_token_cookie', '', expires=0)
return response, 200
except Exception as e:
return jsonify({'error': str(e)}), 500
2024-01-07 14:21:35 +00:00
def login(user):
sessionToken = create_access_token(identity=user.id)
response = jsonify(user.to_dict())
response.set_cookie('access_token_cookie', sessionToken, httponly=True, path='/')
2024-01-09 08:14:28 +00:00
return response # Return the response object with the cookie