File: authentication.md

package info (click to toggle)
strawberry-graphql-django 0.62.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,968 kB
  • sloc: python: 27,530; sh: 17; makefile: 16
file content (47 lines) | stat: -rw-r--r-- 1,268 bytes parent folder | download
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
---
title: Authentication
---

# Authentication

> [!WARNING]
> This solution is enough for web browsers, but will not work for clients that
> doesn't have a way to store cookies in it (e.g. mobile apps). For those it is
> recommended to use token authentication methods. JWT can be used with
> [strawberry-django-auth](https://github.com/nrbnlulu/strawberry-django-auth)
> lib.

`strawberry_django` provides mutations to get authentication going right away.
The `auth.register` mutation performs password validation using Django's `validate_password` method.

```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
```

```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)
```