Coverage for api/decorators.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-05 02:45 +0800

1from functools import wraps 

2from django.http import JsonResponse 

3 

4 

5def api_login_required(view_func): 

6 """Custom login_required decorator for API views that returns JSON 401 instead of redirect.""" 

7 @wraps(view_func) 

8 def _wrapped_view(request, *args, **kwargs): 

9 # Check if user is authenticated 

10 if not request.user.is_authenticated: 

11 return JsonResponse({ 

12 'error': 'Authentication required', 

13 'message': 'You must be logged in to access this API' 

14 }, status=401) 

15 return view_func(request, *args, **kwargs) 

16 return _wrapped_view