Skip to main content

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
from plain.auth import get_request_user

user = get_request_user(request)
if user:
    print(f"Hello, {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.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 authentication
  • plain-oauth - OAuth provider authentication
  • plain-passkeys (TBD) - WebAuthn/passkey authentication
  • plain-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

In templates, use the get_current_user() function:

{% if get_current_user() %}
    <p>Hello, {{ get_current_user().email }}!</p>
{% else %}
    <p>You are not logged in.</p>
{% endif %}

In Python code, use get_request_user():

from plain.auth import get_request_user

user = get_request_user(request)
if user:
    print(f"Hello, {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.user.is_special:
            raise PermissionDenied("You're not special!")

The AuthViewMixin provides:

  • login_required - Requires a logged-in user
  • admin_required - Requires user.is_admin to be True
  • check_auth() - Override for custom authorization logic

Installation

Install the plain.auth package from PyPI:

uv add plain.auth

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plain_auth-0.21.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plain_auth-0.21.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file plain_auth-0.21.0.tar.gz.

File metadata

  • Download URL: plain_auth-0.21.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for plain_auth-0.21.0.tar.gz
Algorithm Hash digest
SHA256 050492b87cb70752bffd1231d44271acc91e3c503b9b90d369cb0163fe87ebd5
MD5 df06a02921de9a85fd6b06aa8ff5c634
BLAKE2b-256 10c1647c2145d20e4ff8b3fb2389d56eb9319f8a1c7f080bf902c140f1ca2a9b

See more details on using hashes here.

File details

Details for the file plain_auth-0.21.0-py3-none-any.whl.

File metadata

  • Download URL: plain_auth-0.21.0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for plain_auth-0.21.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d952fd56e4b7b80287e5292103284ce84795c8aa83f338af89978c8b1029079
MD5 4f42ec3b3ce8b922150ef622d0ecb4ed
BLAKE2b-256 b11dcaa0b85aa9765d1da77c41bc786f4782e8a384e837ab3bc500be3ba2d0b9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page