User Authentication and Authorization
# CHAPTER 12
User Authentication and Authorization
1. Introduction
A major reason companies choose Django is its robust, built-in Authentication system. Building secure login systems, managing user sessions, and cryptographically hashing passwords is incredibly difficult and dangerous to write from scratch. Django provides these features out of the box. In this chapter, we will learn how to create user registration forms, implement login and logout views, and restrict specific pages using authorization decorators.2. Learning Objectives
By the end of this chapter, you will be able to:-
Use Django's built-in
UserCreationFormto register new users.
- Configure Django's built-in Login and Logout views.
- Understand the difference between Authentication and Authorization.
-
Protect Views using the
@loginrequireddecorator.
3. Beginner-Friendly Explanation
Imagine an exclusive country club.- Registration: A person fills out paperwork to become a member. Their name is added to the ledger, and they are given an ID card.
- Authentication (Login): The person shows their ID card to the front gate. The guard verifies they are on the list and lets them onto the grounds.
-
Authorization (Permissions): The person tries to enter the VIP kitchen. The kitchen door has a lock (
@loginrequired). Even though the person is in the club, the kitchen lock checks their specific ID and says, "Employees only. Access Denied."
4. Step 1: User Registration
Django provides a built-inUser model and a pre-built form called UserCreationForm. We don't even need to write forms.py!
In blog/views.py:
*(You will need to create the HTML template users/register.html rendering {{ form.as_p }} just like the previous chapter).*
5. Step 2: Login and Logout Views
Django's login system is so complete that you don't even need to write a View function. You simply import Django's built-in views into yoururls.py.
In core/urls.py:
*(Django expects the login HTML form to use POST and a {% csrf_token %}, similar to your register form).*
6. Step 3: Redirect Configuration
When a user successfully logs in, where should Django send them? You define this in your master settings.In core/settings.py (add to the very bottom):
7. Step 4: Authorization (Protecting Routes)
Now that users can log in, we want to restrict the "Create Post" page. Only authenticated users should be able to write blog posts.In blog/views.py:
8. Backend Workflow: Template Authorization
You can also conditionally display HTML based on the user's authentication status using therequest.user object, which is automatically injected into every template by Django.
In blog/base.html:
9. Best Practices
-
Password Hashing: You will notice we never wrote code to hash the user's password. Django's
UserCreationFormutilizes the PBKDF2 algorithm with a SHA256 hash by default. Never attempt to write your own password hashing algorithm.
10. Common Mistakes
-
Creating a Custom User Model Too Late: For simple apps, the built-in
Usermodel is fine. However, professional apps often require users to log in with anemailinstead of ausername. If you need this, you MUST configure a Custom User Model *before* running your very first migration. Changing to a Custom User Model halfway through a project is a notoriously difficult database nightmare.
11. Exercises
- 1. Define the difference between "Authentication" and "Authorization". Which code snippet in this chapter represents Authorization?
12. Coding Challenges
-
Challenge: Protect the
postdetailview using the@loginrequireddecorator. Test it by opening an Incognito browser window and attempting to navigate directly to/post/1/. You should be redirected to the login page.
13. MCQs with Answers
Which Django feature is used directly above a View function definition to restrict access strictly to users who have logged into the application?
What is the primary benefit of utilizing Django's built-in authviews.LoginView inside your urls.py file?
14. Interview Questions
- Q: Explain how Django manages user sessions under the hood after a successful login. How does the server "remember" the client on subsequent HTTP requests?
- Q: Why is it considered a security risk to store passwords in plain text in a database? How does Django secure passwords by default?
15. FAQs
Q: Can I use third-party logins like "Log in with Google"? A: Yes! This is called OAuth. While Django doesn't do it natively, there is an incredibly popular, industry-standard package calleddjango-allauth that plugs directly into Django to handle Google, GitHub, and Facebook logins seamlessly.
16. Summary
In Chapter 12, we secured our application by implementing user registration and authentication. By leveraging Django's massive suite of built-in tools (UserCreationForm, LoginView, LogoutView), we avoided writing dangerous custom security logic. Furthermore, we implemented strict Authorization rules by protecting our backend routes with the @login_required decorator and dynamically altering our frontend HTML based on the user's session state.