Add endpoint for UserAuthenticationStatus

This commit is contained in:
David Gunter 2017-06-05 20:36:39 -07:00
parent eb9e6eb05a
commit 77231029e6
2 changed files with 23 additions and 1 deletions

View File

@ -2,7 +2,7 @@ from django.conf.urls import url
from rest_auth.views import (
LoginView, LogoutView, UserDetailsView, PasswordChangeView,
PasswordResetView, PasswordResetConfirmView
PasswordResetView, PasswordResetConfirmView, UserAuthenticationStatusView
)
urlpatterns = [
@ -15,6 +15,8 @@ urlpatterns = [
# URLs that require a user to be logged in with a valid session / token.
url(r'^logout/$', LogoutView.as_view(), name='rest_logout'),
url(r'^user/$', UserDetailsView.as_view(), name='rest_user_details'),
url(r'^user/status/$', UserAuthenticationStatusView.as_view(),
name='rest_auth_status'),
url(r'^password/change/$', PasswordChangeView.as_view(),
name='rest_password_change'),
]

View File

@ -153,6 +153,26 @@ class UserDetailsView(RetrieveUpdateAPIView):
return get_user_model().objects.none()
class UserAuthenticationStatusView(APIView):
"""
Checks is_authenticated attribute for User attached to request.
Accepts GET method.
Returns True/False indicator for if user is authenticated.
"""
authentication_classes = ()
permission_classes = ()
def get(self, request, *args, **kwargs):
if hasattr(request, "user") and request.user.is_authenticated:
return Response(
{"authenticated": True}, status=status.HTTP_200_OK
)
return Response(
{"authenticated": False}, status=status.HTTP_401_UNAUTHORIZED
)
class PasswordResetView(GenericAPIView):
"""
Calls Django Auth PasswordResetForm save method.