Skip to main content

Authentication and registration with django graphene and JWT

Project description

https://badge.fury.io/py/graphene-jwt-auth-registration.svg https://travis-ci.org/fivethreeo/graphene-jwt-auth-registration.svg?branch=master https://codecov.io/gh/fivethreeo/graphene-jwt-auth-registration/branch/master/graph/badge.svg

Authentication and registration using graphene and JWT

Documentation

Quickstart

Install Graphene JWT Auth Registration:

pip install graphene-jwt-auth-registration

Add it to your INSTALLED_APPS:

INSTALLED_APPS = [
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sites",

    ...

    "rest_framework",
    "rest_framework_jwt",
    "djoser",
    "graphene_django",

    "gjwt_auth",
]

Set AUTH_USER_MODEL:

AUTH_USER_MODEL = "gjwt_auth.User"

Add the JWTAuthenticationMiddleware:

MIDDLEWARE = [
    ...

    'gjwt_auth.middleware.JWTAuthenticationMiddleware',
]

Create graphene schema in yourproject/schema.py:

import graphene

from gjwt_auth.mutations import (
    Activate,
    DeleteAccount,
    Login,
    RefreshToken,
    Register,
    ResetPassword,
    ResetPasswordConfirm,
)

from gjwt_auth.schema import User, Viewer


class RootQuery(graphene.ObjectType):
    viewer = graphene.Field(Viewer)

    def resolve_viewer(self, info, **kwargs):
        if info.context.user.is_authenticated:
            return info.context.user
        return None


class Mutation(graphene.ObjectType):
    activate = Activate.Field()
    login = Login.Field()
    register = Register.Field()
    deleteAccount = DeleteAccount.Field()
    refreshToken = RefreshToken.Field()
    resetPassword = ResetPassword.Field()
    resetPasswordConfirm = ResetPasswordConfirm.Field()


schema = graphene.Schema(query=RootQuery, mutation=Mutation)

Set the graphene schema:

GRAPHENE = {
    'SCHEMA': 'yourproject.schema.schema'
}

Set djoser setttings:

DJOSER = {
    'DOMAIN': os.environ.get('DJANGO_DJOSER_DOMAIN', 'localhost:3000'),
    'SITE_NAME': os.environ.get('DJANGO_DJOSER_SITE_NAME', 'my site'),
    'PASSWORD_RESET_CONFIRM_URL': '?action=set-new-password&uid={uid}&token={token}',
    'ACTIVATION_URL': 'activate?uid={uid}&token={token}',
    'SEND_ACTIVATION_EMAIL': True,
}

Set jwt auth settings:

JWT_AUTH = {
    'JWT_ALLOW_REFRESH': True,
}

Add Graphene JWT Auth’s URL patterns:

from django.conf.urls import url
from django.views.decorators.csrf import csrf_exempt

from graphene_django.views import GraphQLView

...

urlpatterns = [
    ...
    url(r'^graphql', csrf_exempt(GraphQLView.as_view(graphiql=True))),
    ...
]

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

Credits

Tools used in rendering this package:

History

0.1.0 (2019-06-04)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphene-jwt-auth-registration-0.3.2.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

graphene_jwt_auth_registration-0.3.2-py2.py3-none-any.whl (12.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file graphene-jwt-auth-registration-0.3.2.tar.gz.

File metadata

  • Download URL: graphene-jwt-auth-registration-0.3.2.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.15rc1

File hashes

Hashes for graphene-jwt-auth-registration-0.3.2.tar.gz
Algorithm Hash digest
SHA256 e77d3fc74354625062c142f1a069acea622a9f67dc859ed3a2625fd9f4b3d937
MD5 e4bd698de58f59e4e3696f6148214f38
BLAKE2b-256 1fa7db1f6b7469a664f1bf221c50d83753101e6b974cb3162b78bbab8d1d27a3

See more details on using hashes here.

File details

Details for the file graphene_jwt_auth_registration-0.3.2-py2.py3-none-any.whl.

File metadata

  • Download URL: graphene_jwt_auth_registration-0.3.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.15rc1

File hashes

Hashes for graphene_jwt_auth_registration-0.3.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 063e2591e274939f636872a1bb9e8c08653c524b714226bc7aa689453b958fd2
MD5 fe0d17c2998aa1870d37a4fa8fa23c6f
BLAKE2b-256 1e7987ab5ef46140a6467dc46e833c890e65f870318b2406ef3a2152a4dfde3a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page