JSON Web Token authentication for Django GraphQL.
Fantastic documentation is available at https://github.com/WeenSpace/weenspace-django-jwt.
Install last stable version from Pypi:
pip install weenspace-django-jwt
Add AuthenticationMiddleware
middleware to your MIDDLEWARE settings:
MIDDLEWARE = [
# ...
"django.contrib.auth.middleware.AuthenticationMiddleware",
# ...
]
Add JWTMiddleware
middleware to your GRAPHENE settings:
GRAPHENE = {
"SCHEMA": "mysite.myschema.schema",
"MIDDLEWARE": [
"django_jwt.middleware.JWTMiddleware",
],
}
Add JWTBackend
backend to your AUTHENTICATION_BACKENDS:
AUTHENTICATION_BACKENDS = [
"django_jwt.backends.JWTBackend",
"django.contrib.auth.backends.ModelBackend",
]
Add weenspace-django-jwt mutations to the root schema:
import graphene
import django_jwt
class Mutation(graphene.ObjectType):
create_token = django_jwt.Create.Field()
verify_token = django_jwt.Verify.Field()
refresh_token = django_jwt.Refresh.Field()
schema = graphene.Schema(mutation=Mutation)