Skip to content

Commit 8522511

Browse files
committed
fix: error message of decorator
1 parent f2e588b commit 8522511

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

authentication/decorators.py

+18-28
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,25 @@ def cognito_authenticated(func):
99
@wraps(func)
1010
def wrapper(self, request, *args, **kwargs):
1111
auth = CognitoAuthentication()
12-
user, auth_error = auth.authenticate(request)
13-
if auth_error or not user:
12+
try:
13+
result = auth.authenticate(request)
14+
if result is None:
15+
return Response(
16+
{"error": "Authentication credentials were not provided or are invalid."},
17+
status=status.HTTP_401_UNAUTHORIZED,
18+
)
19+
user, auth_error = result
20+
if auth_error or not user:
21+
return Response(
22+
{"error": "Authentication credentials were not provided or are invalid."},
23+
status=status.HTTP_401_UNAUTHORIZED,
24+
)
25+
request.user = user
26+
return func(self, request, *args, **kwargs)
27+
except Exception as e:
1428
return Response(
15-
{"error": "Authentication credentials were not provided or are invalid."},
16-
status=status.HTTP_401_UNAUTHORIZED,
29+
{"error": f"Authentication failed: {str(e)}"},
30+
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
1731
)
18-
request.user = user
19-
return func(self, request, *args, **kwargs)
2032

2133
return wrapper
22-
23-
24-
# from functools import wraps
25-
# from rest_framework.response import Response
26-
# from rest_framework import status
27-
# from rest_framework.decorators import api_view, authentication_classes
28-
# from .services.cognito_authentication import CognitoAuthentication
29-
30-
31-
# def cognito_authenticated(func):
32-
# @wraps(func)
33-
# @api_view(["GET", "POST", "PUT", "DELETE"])
34-
# @authentication_classes([CognitoAuthentication])
35-
# def wrapper(request, *args, **kwargs):
36-
# if not request.user or not hasattr(request.user, "get"):
37-
# return Response(
38-
# {"error": "Authentication credentials were not provided or are invalid."},
39-
# status=status.HTTP_401_UNAUTHORIZED,
40-
# )
41-
# return func(request, *args, **kwargs)
42-
43-
# return wrapper

0 commit comments

Comments
 (0)