Skip to content

Commit 67d398a

Browse files
committed
docs on django built-in authentication
1 parent 9e1e5f6 commit 67d398a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/docs/guides/authentication.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,48 @@ Note: **`param_name`** is the name of the GET parameter that will be checked for
126126
{!./src/tutorial/authentication/apikey03.py!}
127127
```
128128

129+
### Django Session Authentication
130+
131+
**Django Ninja** provides built-in session authentication classes that leverage Django's existing session framework:
132+
133+
#### SessionAuth
134+
135+
Uses Django's default session authentication - authenticates any logged-in user:
136+
137+
```python
138+
from ninja.security import SessionAuth
139+
140+
@api.get("/protected", auth=SessionAuth())
141+
def protected_view(request):
142+
return {"user": request.auth.username}
143+
```
144+
145+
#### SessionAuthSuperUser
146+
147+
Authenticates only users with superuser privileges:
148+
149+
```python
150+
from ninja.security import SessionAuthSuperUser
151+
152+
@api.get("/admin-only", auth=SessionAuthSuperUser())
153+
def admin_view(request):
154+
return {"message": "Hello superuser!"}
155+
```
156+
157+
#### SessionAuthIsStaff
158+
159+
Authenticates users who are either superusers or staff members:
160+
161+
```python
162+
from ninja.security import SessionAuthIsStaff
163+
164+
@api.get("/staff-area", auth=SessionAuthIsStaff())
165+
def staff_view(request):
166+
return {"message": "Hello staff member!"}
167+
```
168+
169+
These authentication classes automatically use Django's `SESSION_COOKIE_NAME` setting and check the user's authentication status through the standard Django session framework.
170+
129171

130172

131173
### HTTP Bearer

0 commit comments

Comments
 (0)