From b63cb772e46364205b0c2c932399abd022df6e4e Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Thu, 9 Feb 2017 18:18:50 +0200 Subject: [PATCH] Add `Adding login required` section to authorization --- docs/authorization.rst | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/docs/authorization.rst b/docs/authorization.rst index 9ee7d0a..f003a01 100644 --- a/docs/authorization.rst +++ b/docs/authorization.rst @@ -1,7 +1,7 @@ Authorization in Django ======================= -There are two main ways you may want to limit access to data when +There are several ways you may want to limit access to data when working with Graphene and Django: limiting which fields are accessible via GraphQL and limiting which objects a user can access. @@ -108,3 +108,28 @@ method to your ``DjangoObjectType``. if post.published or context.user == post.owner: return post return None + +Adding login required +--------------------- + +If you want to use the standard Django LoginRequiredMixin_ you can create your own view, which includes the ``LoginRequiredMixin`` and subclasses the ``GraphQLView``: + +.. code:: python + + from django.contrib.auth.mixins import LoginRequiredMixin + from graphene_django.views import GraphQLView + + + class PrivateGraphQLView(LoginRequiredMixin, GraphQLView): + pass + +After this, you can use the new ``PrivateGraphQLView`` in ``urls.py``: + +.. code:: python + + urlpatterns = [ + # some other urls + url(r'^graphql', PrivateGraphQLView.as_view(graphiql=True, schema=schema)), + ] + +.. _LoginRequiredMixin: https://docs.djangoproject.com/en/1.10/topics/auth/default/#the-loginrequired-mixin