Added me/profile page and small improvements
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(),
|
||||
)
|
||||
]
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user