From a8c63e223a78dfa62848d3db8c97c750baa31ba4 Mon Sep 17 00:00:00 2001 From: ITQ Date: Thu, 29 Feb 2024 22:47:48 +0300 Subject: [PATCH] Added me/profile page and small improvements --- solution/pulse/pulse/urls.py | 2 +- solution/pulse/users/authentication.py | 13 ++++++++----- solution/pulse/users/models.py | 6 +++--- solution/pulse/users/urls.py | 8 ++++---- solution/pulse/users/views.py | 19 ++++++++++++++++--- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/solution/pulse/pulse/urls.py b/solution/pulse/pulse/urls.py index bb94c95..1d45e78 100644 --- a/solution/pulse/pulse/urls.py +++ b/solution/pulse/pulse/urls.py @@ -15,7 +15,7 @@ urlpatterns = [ # API path("api/ping", include("ping.urls")), path("api/countries", include("countries.urls")), - path("api/auth/", include("users.urls")), + path("api/", include("users.urls")), ] if settings.DEBUG: diff --git a/solution/pulse/users/authentication.py b/solution/pulse/users/authentication.py index a460d73..5e41c6c 100644 --- a/solution/pulse/users/authentication.py +++ b/solution/pulse/users/authentication.py @@ -19,11 +19,14 @@ class JWTAuthentication(BaseAuthentication): ) user = Profile.objects.get(login=payload["login"]) - - return (user, None) except Profile.DoesNotExist: - raise AuthenticationFailed("Invalid token") + error = "Invalid token" + raise AuthenticationFailed(error) from None except jwt.ExpiredSignatureError: - raise AuthenticationFailed("Token has expired") + error = "Token has expired" + raise AuthenticationFailed(error) from None except jwt.InvalidTokenError: - raise AuthenticationFailed("Invalid token") + error = "Invalid token" + raise AuthenticationFailed(error) from None + else: + return (user, None) diff --git a/solution/pulse/users/models.py b/solution/pulse/users/models.py index ad878d7..0ca42a4 100644 --- a/solution/pulse/users/models.py +++ b/solution/pulse/users/models.py @@ -34,8 +34,8 @@ class Profile(models.Model): ) image = models.URLField(max_length=200, blank=True, null=True) - def is_authenticated(self): - return True - def __str__(self): return self.login + + def is_authenticated(self): + return True diff --git a/solution/pulse/users/urls.py b/solution/pulse/users/urls.py index 6b643d9..a096b2f 100644 --- a/solution/pulse/users/urls.py +++ b/solution/pulse/users/urls.py @@ -4,17 +4,17 @@ import users.views urlpatterns = [ path( - "register", + "auth/register", users.views.RegisterUserApiView.as_view(), name="register", ), path( - "sign-in", + "auth/sign-in", users.views.SigninUserApiView.as_view(), name="sign-in", ), path( - "protected-view", - users.views.ProtectedView.as_view(), + "me/profile", + users.views.ProfileMeApiView.as_view(), ) ] diff --git a/solution/pulse/users/views.py b/solution/pulse/users/views.py index 9c9ef99..39f8144 100644 --- a/solution/pulse/users/views.py +++ b/solution/pulse/users/views.py @@ -60,7 +60,7 @@ class RegisterUserApiView(APIView): if not (bool(re.match(password_pattern, password))): error = { - "message": "Your password does not meet our requirements" + "error": "Your password does not meet our requirements" } return Response( error, @@ -125,9 +125,22 @@ class SigninUserApiView(APIView): return Response({"token": token}) -class ProtectedView(APIView): +class ProfileMeApiView(APIView): permission_classes = [IsAuthenticated] def get(self, request): user = request.user - return Response({"message": "Authenticated", "user": str(user)}) + + profile = { + "login": user.login, + "email": user.email, + "countryCode": user.countryCode, + "isPublic": user.isPublic, + } + + if user.phone is not None: + profile["phone"] = user.phone + if user.image is not None: + profile["image"] = user.image + + return Response(profile)