Skip to content

VachaganGrigoryan/jwtberry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jwtberry

jwtberry Logo

jwtberry is a Python package that simplifies JWT (JSON Web Token) authentication for Django Strawberry GraphQL projects. It provides easy-to-use tools for implementing JWT authentication within your Strawberry-based GraphQL API.

Features

  • Seamless integration with Django Strawberry GraphQL.
  • Simplified JWT authentication setup for your API.
  • Customizable token generation and validation.

Installation

You can install jwtberry using pip:

pip install jwtberry

Usage

  1. Add 'jwtberry.blackberry' app to your Django INSTALLED_APPS in the settings.py file:
INSTALLED_APPS = [
    # ...
    'jwtberry.blackberry',
]
  1. Configure your Django project's AUTHENTICATION_BACKENDS in settings.py to include the JwtAuthBackend from jwtberry:
AUTHENTICATION_BACKENDS = [
    "jwtberry.backends.JwtAuthBackend",
    "django.contrib.auth.backends.ModelBackend",
]
  1. Update your project's urls.py by importing JwtAsyncGraphQLView from jwtberry.views and adding a path for the GraphQL endpoint:
from jwtberry.views import JwtAsyncGraphQLView

urlpatterns = [
    # ...
    path("graphql/", JwtAsyncGraphQLView.as_view(schema=schema)),
]
  1. In your schema.py (where you define your GraphQL schema), make the following changes to include JWT-based authentication:
import strawberry
from jwtberry.mutations import auth_token
from jwtberry.permission import IsAuthenticated
from jwtberry.types import JwtAuthResponse

@strawberry.type
class Query:
    hi: str = strawberry.field(resolver=lambda: "Hello", permission_classes=[IsAuthenticated])

@strawberry.type
class Mutation:
    login: JwtAuthResponse = auth_token

schema = strawberry.Schema(
    query=Query,
    mutation=Mutation,
)

IsAuthenticated is a permission class that checks whether the user has authenticated by verifying the Authorization header.

For more detailed information and examples, check the example project.

About

JWT Authentication for Django Strawberry GraphQl

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages