@@ -9,35 +9,25 @@ def cognito_authenticated(func):
9
9
@wraps (func )
10
10
def wrapper (self , request , * args , ** kwargs ):
11
11
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 :
14
28
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 ,
17
31
)
18
- request .user = user
19
- return func (self , request , * args , ** kwargs )
20
32
21
33
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