User authentication and authorization for Plain.
Project description
plain.auth
Add users to your app and define which views they can access.
To log a user in, you'll want to pair this package with:
plain-passwordsplain-oauthplain-passkeys(TBD)plain-passlinks(TBD)
Installation
# app/settings.py
INSTALLED_PACKAGES = [
# ...
"plain.auth",
"plain.sessions",
"plain.passwords",
]
MIDDLEWARE = [
"plain.sessions.middleware.SessionMiddleware",
"plain.auth.middleware.AuthenticationMiddleware",
]
AUTH_USER_MODEL = "users.User"
AUTH_LOGIN_URL = "login"
Create your own user model (plain create users).
# app/users/models.py
from plain import models
from plain.passwords.models import PasswordField
class User(models.Model):
email = models.EmailField()
password = PasswordField()
is_admin = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.email
Define your URL/view where users can log in.
# app/urls.py
from plain.auth.views import LoginView, LogoutView
from plain.urls import include, path
from plain.passwords.views import PasswordLoginView
class LoginView(PasswordLoginView):
template_name = "login.html"
urlpatterns = [
path("logout/", LogoutView, name="logout"),
path("login/", LoginView, name="login"),
]
Checking if a user is logged in
A request.user will either be None or point to an instance of a your AUTH_USER_MODEL.
So in templates you can do:
{% if request.user %}
<p>Hello, {{ request.user.email }}!</p>
{% else %}
<p>You are not logged in.</p>
{% endif %}
Or in Python:
if request.user:
print(f"Hello, {request.user.email}!")
else:
print("You are not logged in.")
Restricting views
Use the AuthViewMixin to restrict views to logged in users, admin users, or custom logic.
from plain.auth.views import AuthViewMixin
from plain.exceptions import PermissionDenied
from plain.views import View
class LoggedInView(AuthViewMixin, View):
login_required = True
class AdminOnlyView(AuthViewMixin, View):
login_required = True
admin_required = True
class CustomPermissionView(AuthViewMixin, View):
def check_auth(self):
super().check_auth()
if not self.request.user.is_special:
raise PermissionDenied("You're not special!")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file plain_auth-0.13.0.tar.gz.
File metadata
- Download URL: plain_auth-0.13.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.7.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b23c3d02ff9d3cd2aa0326ceff38a730671565b06933eea9cbd32383582092c
|
|
| MD5 |
a523a33daa1bdbb6ac40b141fa551428
|
|
| BLAKE2b-256 |
015d2640dce2619ff9c14858c9d38f512e7b918884bc88b9c46cf954553eed0d
|
File details
Details for the file plain_auth-0.13.0-py3-none-any.whl.
File metadata
- Download URL: plain_auth-0.13.0-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.7.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f53b29ad4634f92b6ed0ef627670ab1a153ad0ad3ceff5a973fce974efa4dacb
|
|
| MD5 |
a96bf3b4b9bacadcfd60a1fec2456fbd
|
|
| BLAKE2b-256 |
c06c76c69c1fddb8a44ce65edb8e874904f13ab02194d56ed33d2b3bf703b139
|