Add users to your app and decide what they can access.
Project description
plain.auth
Add users to your app and decide what they can access.
Overview
The plain.auth package provides user authentication and authorization for Plain applications. Here's a basic example of checking if a user is logged in:
# In a view
if request.user:
print(f"Hello, {request.user.email}!")
else:
print("You are not logged in.")
And restricting a view to logged-in users:
from plain.auth.views import AuthViewMixin
from plain.views import View
class ProfileView(AuthViewMixin, View):
login_required = True
def get(self):
return f"Welcome, {self.request.user.email}!"
Authentication setup
Settings configuration
Configure your authentication settings in app/settings.py:
INSTALLED_PACKAGES = [
# ...
"plain.auth",
"plain.sessions",
"plain.passwords", # Or another auth method
]
MIDDLEWARE = [
"plain.sessions.middleware.SessionMiddleware",
]
AUTH_USER_MODEL = "users.User"
AUTH_LOGIN_URL = "login"
Creating a user model
Create your own user model using plain create users or manually:
# 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
Login views
To log users in, you'll need to pair this package with an authentication method:
plain-passwords- Username/password authenticationplain-oauth- OAuth provider authenticationplain-passkeys(TBD) - WebAuthn/passkey authenticationplain-passlinks(TBD) - Magic link authentication
Example with password authentication:
# app/urls.py
from plain.auth.views import LogoutView
from plain.urls import 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 your AUTH_USER_MODEL.
In templates:
{% if request.user %}
<p>Hello, {{ request.user.email }}!</p>
{% else %}
<p>You are not logged in.</p>
{% endif %}
In Python code:
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!")
The AuthViewMixin provides:
login_required- Requires a logged-in useradmin_required- Requiresuser.is_adminto be Truecheck_auth()- Override for custom authorization logic
Installation
Install the plain.auth package from PyPI:
uv add plain.auth
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.20.0.tar.gz.
File metadata
- Download URL: plain_auth-0.20.0.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6fb4143b431a51aa9f79ef0f30f8a450fbd087bc3db678c218c33571ee623091
|
|
| MD5 |
e93ed5f70f05839da6e2f5e2b651419c
|
|
| BLAKE2b-256 |
031f68a8c3b47dc79807144816c819157ad8fc2e04fa7e69829d11b763f39c93
|
File details
Details for the file plain_auth-0.20.0-py3-none-any.whl.
File metadata
- Download URL: plain_auth-0.20.0-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a05cbe44cbb5e43f25cb40a6b6740a63ed0336ffb2b22b1afaf35deb688d6c70
|
|
| MD5 |
6f4a2fb39fb1b00290314d2dd70a6ea3
|
|
| BLAKE2b-256 |
b05bdfe4eced7a7ce5e54d7f7cb52f4384c3caadb3fce6b5c4a45a5e6e8dfa8d
|