Authentication

strawberry_django provides built-in mutations and queries for session-based authentication with Django’s authentication system.

Warning

This solution is designed for web browsers that support cookies. It will not work for clients that can’t store cookies (e.g., mobile apps). For those scenarios, use token-based authentication methods like JWT with strawberry-django-auth .

Quick Start

Define Types

types.py
import strawberry_django
from strawberry import auto
from django.contrib.auth import get_user_model
@strawberry_django.type(get_user_model())
class User:
username: auto
email: auto
@strawberry_django.input(get_user_model())
class UserInput:
username: auto
password: auto
email: auto # Optional: add other fields as needed

Define Schema

schema.py
import strawberry
import strawberry_django
from .types import User, UserInput
@strawberry.type
class Query:
me: User = strawberry_django.auth.current_user()
@strawberry.type
class Mutation:
login: User = strawberry_django.auth.login()
logout = strawberry_django.auth.logout()
register: User = strawberry_django.auth.register(UserInput)

Available Functions

current_user()

A field that returns the currently authenticated user.

me: User = strawberry_django.auth.current_user()

Behavior:

GraphQL Usage:

query {
me {
username
email
}
}

login()

A mutation that authenticates a user with username and password.

login: User = strawberry_django.auth.login()

Arguments (automatically generated):

Behavior:

GraphQL Usage:

mutation {
login(username: "myuser", password: "mypassword") {
username
email
}
}

logout()

A mutation that logs out the current user.

logout = strawberry_django.auth.logout()

Behavior:

GraphQL Usage:

mutation {
logout
}

register(input_type)

A mutation that creates a new user account.

register: User = strawberry_django.auth.register(UserInput)

Arguments:

Behavior:

GraphQL Usage:

mutation {
register(
data: {
username: "newuser"
password: "securepassword123"
email: "user@example.com"
}
) {
username
email
}
}

Password Validation

The register mutation automatically validates passwords against Django’s AUTH_PASSWORD_VALIDATORS . Configure validators in your settings:

settings.py
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {
'min_length': 8,
}
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

Using with Custom User Models

The auth functions work with custom user models. Ensure your type and input reference the correct model:

types.py
from django.contrib.auth import get_user_model
@strawberry_django.type(get_user_model())
class User:
# Your custom user fields
username: auto
email: auto
first_name: auto
last_name: auto

Optional User Return Type

You can make login return None on failure instead of raising an error:

@strawberry.type
class Mutation:
login: User | None = strawberry_django.auth.login()

This way, unsuccessful logins return null instead of a GraphQL error.

Accessing User in Resolvers

You can access the current user in any resolver:

from strawberry.types import Info
@strawberry.type
class Query:
@strawberry.field
def my_data(self, info: Info) -> str:
user = info.context.request.user
if not user.is_authenticated:
raise PermissionError("Not authenticated")
return f"Hello, {user.username}!"

Or use the utility function:

from strawberry_django.auth.utils import get_current_user
@strawberry.field
def my_data(self, info: Info) -> str:
user = get_current_user(info)
# ...

Django Channels Support

The login and logout mutations automatically detect Django Channels and use the appropriate authentication methods:

This allows authentication to work seamlessly with subscriptions .

Session Configuration

Ensure your Django session settings are properly configured:

settings.py
# Required middleware
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
# ...
]
# Session settings
SESSION_ENGINE = 'django.contrib.sessions.backends.db' # Or cache, file, etc.
SESSION_COOKIE_SECURE = True # For HTTPS
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = 'Lax' # Or 'Strict' for more security

Error Handling

Authentication errors are raised as ValidationError :

from django.core.exceptions import ValidationError
# Login failure
ValidationError("Incorrect username/password")
# Not logged in (current_user)
ValidationError("User is not logged in.")
# Password validation failure (register)
ValidationError("This password is too short...")

You can catch these in your frontend or use error handling extensions .

See Also