1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
---
title: Authentication
---
# 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](https://github.com/nrbnlulu/strawberry-django-auth).
## Quick Start
### Define Types
```python title="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
```python title="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.
```python
me: User = strawberry_django.auth.current_user()
```
**Behavior:**
- Returns the authenticated user object
- Raises `ValidationError("User is not logged in.")` if the user is not authenticated
**GraphQL Usage:**
```graphql
query {
me {
username
email
}
}
```
### `login()`
A mutation that authenticates a user with username and password.
```python
login: User = strawberry_django.auth.login()
```
**Arguments (automatically generated):**
- `username: String!` - The username
- `password: String!` - The password
**Behavior:**
- Uses Django's `authenticate()` to verify credentials
- Creates a session using Django's `login()`
- Supports both WSGI and ASGI (including Django Channels)
- Returns the authenticated user on success
- Raises `ValidationError("Incorrect username/password")` on failure
**GraphQL Usage:**
```graphql
mutation {
login(username: "myuser", password: "mypassword") {
username
email
}
}
```
### `logout()`
A mutation that logs out the current user.
```python
logout = strawberry_django.auth.logout()
```
**Behavior:**
- Ends the current session using Django's `logout()`
- Supports both WSGI and ASGI (including Django Channels)
- Returns `true` if a user was logged out, `false` if no user was logged in
**GraphQL Usage:**
```graphql
mutation {
logout
}
```
### `register(input_type)`
A mutation that creates a new user account.
```python
register: User = strawberry_django.auth.register(UserInput)
```
**Arguments:**
- `input_type` - A strawberry_django input type for user creation
**Behavior:**
- Validates the password using Django's `validate_password()` (checks against `AUTH_PASSWORD_VALIDATORS`)
- Creates the user with a properly hashed password using `set_password()`
- Returns the created user object
- Raises validation errors if password doesn't meet requirements
**GraphQL Usage:**
```graphql
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:
```python title="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:
```python title="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:
```python
@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:
```python
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:
```python
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:
- For standard WSGI/ASGI: Uses `django.contrib.auth.login/logout`
- For Channels WebSocket: Uses `channels.auth.login/logout`
This allows authentication to work seamlessly with [subscriptions](./subscriptions.md).
## Session Configuration
Ensure your Django session settings are properly configured:
```python title="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`:
```python
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](./error-handling.md).
## See Also
- [Permissions](./permissions.md) - Protecting fields and operations
- [Django Channels](../integrations/channels.md) - WebSocket authentication setup
- [strawberry-django-auth](https://github.com/nrbnlulu/strawberry-django-auth) - JWT authentication
|