from rest_framework_simplejwt.authentication import JWTAuthentication
from .models import User
from utils.helper import failed_response

class UserJWTAuthentication(JWTAuthentication):
    def get_user(self, validated_token):
        user_id = validated_token.get("user_id")
        if user_id is None:
            raise failed_response(msg="User ID not found in token")
        try:
            user = User.objects.get(id=user_id)
        except User.DoesNotExist:
            raise failed_response(msg=f"No such user found with ID {user_id}")
        return user
