diff --git a/rest_auth/urls.py b/rest_auth/urls.py index 7a35e9b..b752eaf 100644 --- a/rest_auth/urls.py +++ b/rest_auth/urls.py @@ -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'), ] diff --git a/rest_auth/views.py b/rest_auth/views.py index 0493a76..454b450 100644 --- a/rest_auth/views.py +++ b/rest_auth/views.py @@ -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.