Authentication and the User Model
The auth is the user the no the code: the login (the authenticate, the session, the no the 1 the token), the logout (the flush, the cookie the no), and the @login_required (the no the login, the redirect the next). The auth views is the 6 (the LoginView, the logout, the password the 4), the session is the signed (the cookie, the no the tamper), and the custom user is the 1 the decision (the email, the no the username). This chapter covers the authenticate (the 3 the step, the no the hash), the auth views (the 6, the no the code), the @login_required (the gate, the next), and the custom user (the email, the 1 the decision).
The login: The 3 step
# the LOGIN (the 3, the no the hash):
from django.contrib.auth import (
authenticate, login, logout)
def my_login(request):
if request.method == "POST":
# the 1: the authenticate (the
# the verify, the no the
# the 1):
user = authenticate(
request,
username=request.POST.get(
"username", ""),
password=request.POST.get(
"password", ""))
# the no: the no (the
# the password, the no
# the 500)
if user is not None:
# the 2: the login (the
# the session, the
# the signed, the no
# the cookie's):
login(request, user)
# the 3: the redirect
# (the next, the
# the no the again):
next_url = request.GET.get(
"next", "/notes/")
return HttpResponseRedirect(
next_url)
# the no: the form (the
# the no, the no the data
# the no):
return render(request,
"notes/login.html")
# the LOGOUT (the flush, the cookie's
# the no):
def my_logout(request):
if request.method == "POST":
logout(request) # the
# the session
return HttpResponseRedirect(
"/accounts/login/")
# the no: the GET (the CSRF's,
# the 2 the no, the no
# the no)
The login is the 3: the authenticate (the username plus the password, the hash the check, the no the 1, the None the no the user or the password the no), the login (the session the key, the signed the cookie, the no the tamper), and the redirect (the next the GET, the no the again, the 12factor) - the hash is the no the 1 (the PBKDF2, the iter the 600K, the plain the no the DB, the no the the the read). The logout is the flush: the session the no, the cookie the delete, and the POST is the yes (the CSRF the GET the no, the 2 the no the click) - the request.user is the no (the is_authenticated the check, the AnonymousUser the no the login, the is_staff the admin) - the auth views is the next: the 6 the no the code.
The auth views, the decorator, and the custom user
# the AUTH VIEWS (the 6, the no code):
# the 1: the LoginView (the form,
# the redirect, the 1 the line):
# the urls:
# the 1: the 3 import
from django.contrib.auth import \
views as auth_views
from django.urls import path
urlpatterns = [
path("accounts/login/",
auth_views.LoginView.as_view(
template_name="notes/login.html"),
name="login"),
# the 2: the logout (the POST,
# the no the GET):
path("accounts/logout/",
auth_views.LogoutView.as_view(),
name="logout"),
# the 3: the password (the 4,
# the no the form, the
# the email, the no
# the 1):
path("accounts/password_change/",
auth_views.PasswordChangeView.as_view(),
name="password_change"),
path("accounts/password_reset/",
auth_views.PasswordResetView.as_view(
email_template_name=(
"notes/email_reset.txt")),
name="password_reset"),
path("accounts/reset/<uidb64>/"
"<token>/",
auth_views.PasswordResetConfirmView.as_view(),
name="password_reset_confirm"),
path("accounts/reset/done/",
auth_views.PasswordResetCompleteView.as_view(),
name="password_reset_complete"),
]
# the no: the 1 (the form, the
# the email, the no the 3)
# the DECORATOR (the gate, the next):
from django.contrib.auth.decorators import \
login_required
from django.views.decorators.http import \
require_POST
@login_required
@require_POST
def note_delete(request, pk):
Note.objects.filter(
pk=pk).delete()
return HttpResponseRedirect(
"/notes/")
# the no: the login the
# the no (the redirect, the
# the next, the no the 403)
# the CUSTOM USER (the email, the
# 1 decision, the no the later):
# the models:
# the 1: the 1 import
from django.contrib.auth.models import \
AbstractUser
class User(AbstractUser):
# the no: the username (the
# the email, the 1, the no
# the 2):
username = None
email = models.EmailField(
unique=True)
USERNAME_FIELD = "email"
REQUIRED_FIELDS = []
# the settings (the 1, the no
# the later, the no the
# the 1):
# the AUTH_USER_MODEL =
# "notes_app.User"
The auth views is the 6: the LoginView (the form, the redirect the next, the template the 1), the LogoutView (the POST, the no the GET), and the password the 4 (the change, the reset, the confirm, the complete, the email the no the 1) - the no is the 3 (the form, the email, the the the no). The @login_required is the gate: the no the login is the redirect (the /accounts/login/?next=, the no the 403), and the @require_POST is the method (the 405, the no the if) - the custom user is the 1 the decision: the AbstractUser (the subclass, the no the swap), the email is the USERNAME_FIELD (the 1, the no the 2), and the AUTH_USER_MODEL is the 1 (the 1st the 1, the no the later, the migration the no) - the rule is the one: the authenticate the 3, the auth views the 6, the @login_required the gate, and the custom user the 1 - the next chapter's permission is the 4 the object the no the 403.
The Working Rules
- The login is the 3, the authenticate the no. The username plus the password (the hash the check, the no the 1, the None the no), the login the session (the signed, the no the tamper), and the redirect the next (the no the again) - the PBKDF2 the 600K (the plain the no the DB, the no the read), and the no the 500 (the user the no, the no the stack).
- The auth views is the 6, the no code. The LoginView the form (the next, the template), the LogoutView the POST (the no the GET, the CSRF), and the password the 4 (the change, the reset, the confirm, the complete) - the no the 3 (the form, the email, the no the 1), and the no the login.html (the template, the no the default).
- The @login_required is the gate, the next the redirect. The no the login (the /accounts/login/?next=, the no the 403), and the @require_POST the 405 (the no the if) - the no the 2 (the login, the POST, the no the 3), and the no the view (the gate the no, the logic the no).
- The custom user is the 1, the AbstractUser the no swap. The email the USERNAME_FIELD (the 1, the no the 2), and the AUTH_USER_MODEL the 1st the 1 (the no the later, the migration the no) - the no the username (the unique, the no the email), and the no the 2 (the 1 the user, the no the 2 the model).
Summary
The auth is the user the no code: the login the 3 (the authenticate the hash the no the 1 the None, the login the session the signed the no the tamper, the redirect the next), the logout the flush (the session the no, the POST the no the GET), and the auth views the 6 (the LoginView the next, the LogoutView the POST, the password the 4 the email the no the 1). The @login_required is the gate (the no the login the redirect the next, the no the 403), the @require_POST is the 405, and the custom user is the 1 decision (the AbstractUser the no the swap, the email the USERNAME_FIELD, the AUTH_USER_MODEL the 1st the 1, the no the later). The hash is the PBKDF2 (the 600K the iter, the plain the no the DB) - the next chapter's permission is the 4: the model the add, the object the no, and the 403 the no the redirect.