|
2 | 2 |
|
3 | 3 | # bolt-auth
|
4 | 4 |
|
5 |
| -Create users and authenticate them. |
| 5 | +Add users to your app and define which views they can access. |
6 | 6 |
|
7 |
| -[link](https://boltframework.dev/docs/bolt-auth/bolt/auth/config.py) |
| 7 | +To log a user in, you'll want to pair this package with: |
8 | 8 |
|
9 |
| -## Installation |
| 9 | +- `bolt-passwords` |
| 10 | +- `bolt-oauth` |
| 11 | +- `bolt-passkeys` (TBD) |
| 12 | +- `bolt-passlinks` (TBD) |
10 | 13 |
|
11 |
| -- install bolt-auth |
12 |
| -- install bolt-sessions |
13 |
| -- optionally bolt-passwords, etc. |
14 |
| -- add bolt.auth to installed packages |
| 14 | +## Installation |
15 | 15 |
|
16 | 16 | ```python
|
| 17 | +# app/settings.py |
17 | 18 | INSTALLED_PACKAGES = [
|
18 | 19 | # ...
|
19 | 20 | "bolt.auth",
|
| 21 | + "bolt.sessions", |
| 22 | + "bolt.passwords", |
20 | 23 | ]
|
21 |
| -``` |
22 | 24 |
|
23 |
| -``` |
24 |
| -# settings.py |
25 | 25 | MIDDLEWARE = [
|
26 | 26 | "bolt.middleware.security.SecurityMiddleware",
|
27 | 27 | "bolt.assets.whitenoise.middleware.WhiteNoiseMiddleware",
|
28 |
| - "bolt.sessions.middleware.SessionMiddleware", # <-- Add SessionMiddleware |
| 28 | + "bolt.sessions.middleware.SessionMiddleware", # <-- |
29 | 29 | "bolt.middleware.common.CommonMiddleware",
|
30 | 30 | "bolt.csrf.middleware.CsrfViewMiddleware",
|
31 |
| - "bolt.auth.middleware.AuthenticationMiddleware", # <-- Add AuthenticationMiddleware |
| 31 | + "bolt.auth.middleware.AuthenticationMiddleware", # <-- |
32 | 32 | "bolt.middleware.clickjacking.XFrameOptionsMiddleware",
|
33 | 33 | ]
|
| 34 | + |
| 35 | +AUTH_USER_MODEL = "users.User" |
| 36 | +AUTH_LOGIN_URL = "login" |
| 37 | +``` |
| 38 | + |
| 39 | +Create your own user model (`bolt create users`). |
| 40 | + |
| 41 | +```python |
| 42 | +# app/users/models.py |
| 43 | +from bolt.db import models |
| 44 | +from bolt.passwords.models import PasswordField |
| 45 | + |
| 46 | + |
| 47 | +class User(models.Model): |
| 48 | + email = models.EmailField(unique=True) |
| 49 | + password = PasswordField() |
| 50 | + is_staff = models.BooleanField(default=False) |
| 51 | + created_at = models.DateTimeField(auto_now_add=True) |
| 52 | + |
| 53 | + def __str__(self): |
| 54 | + return self.email |
| 55 | +``` |
| 56 | + |
| 57 | +Define your URL/view where users can log in. |
| 58 | + |
| 59 | +```python |
| 60 | +# app/urls.py |
| 61 | +from bolt.auth.views import LoginView, LogoutView |
| 62 | +from bolt.urls import include, path |
| 63 | +from bolt.passwords.views import PasswordLoginView |
| 64 | + |
| 65 | + |
| 66 | +class LoginView(PasswordLoginView): |
| 67 | + template_name = "login.html" |
| 68 | + |
| 69 | + |
| 70 | +urlpatterns = [ |
| 71 | + path("logout/", LogoutView, name="logout"), |
| 72 | + path("login/", LoginView, name="login"), |
| 73 | +] |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | +## Checking if a user is logged in |
| 78 | + |
| 79 | +A `request.user` will either be `None` or point to an instance of a your `AUTH_USER_MODEL`. |
| 80 | + |
| 81 | +So in templates you can do: |
| 82 | + |
| 83 | +```html |
| 84 | +{% if request.user %} |
| 85 | + <p>Hello, {{ request.user.email }}!</p> |
| 86 | +{% else %} |
| 87 | + <p>You are not logged in.</p> |
| 88 | +{% endif %} |
| 89 | +``` |
| 90 | + |
| 91 | +Or in Python: |
| 92 | + |
| 93 | +```python |
| 94 | +if request.user: |
| 95 | + print(f"Hello, {request.user.email}!") |
| 96 | +else: |
| 97 | + print("You are not logged in.") |
| 98 | +``` |
| 99 | + |
| 100 | + |
| 101 | +## Restricting views |
| 102 | + |
| 103 | +Use the `AuthViewMixin` to restrict views to logged in users, staff users, or custom logic. |
| 104 | + |
| 105 | +```python |
| 106 | +from bolt.auth.views import AuthViewMixin |
| 107 | +from bolt.exceptions import PermissionDenied |
| 108 | +from bolt.views import View |
| 109 | + |
| 110 | + |
| 111 | +class LoggedInView(AuthViewMixin, View): |
| 112 | + login_required = True |
| 113 | + |
| 114 | + |
| 115 | +class StaffOnlyView(AuthViewMixin, View): |
| 116 | + login_required = True |
| 117 | + staff_required = True |
| 118 | + |
| 119 | + |
| 120 | +class CustomPermissionView(AuthViewMixin, View): |
| 121 | + def check_auth(self): |
| 122 | + super().check_auth() |
| 123 | + if not self.request.user.is_special: |
| 124 | + raise PermissionDenied("You're not special!") |
34 | 125 | ```
|
0 commit comments