Create a Blueprint
A Blueprint is a way to organize a group of related views and other code. Rather than registering views directly with an application, they are registered with a blueprint. Then the blueprint is registered with the application when it is available in the factory function. Flaskr will have two blueprints:- Authentication functions
- Blog posts functions
Authentication Blueprint
Create the auth blueprint
Create This creates a Blueprint named
flaskr/auth.py:flaskr/auth.py
'auth'. The url_prefix will be prepended to all URLs associated with the blueprint.The Register View
When the user visits/auth/register, the register view will return HTML with a form for them to fill out. When they submit the form, it will validate their input and either show the form again with an error message or create the new user and go to the login page.
flaskr/auth.py
How It Works
-
@bp.route('/register', methods=('GET', 'POST'))- Associates the URL/registerwith theregisterview function -
Validation - Checks that
usernameandpasswordare not empty -
Database Insert - Uses parameterized queries with
?placeholders to prevent SQL injection attacks -
Password Security -
generate_password_hash()securely hashes the password before storing it -
Error Handling - If the username already exists,
IntegrityErroris caught and shown to the user -
Redirect - After successful registration,
redirect()sends the user to the login page.url_for()generates the URL based on the view name -
Flash Messages -
flash()stores messages that can be retrieved when rendering the template
The Login View
The login view follows the same pattern as the register view:flaskr/auth.py
Differences from Register
- The user is queried first and stored in a variable for later use
-
fetchone()returns one row from the query. If the query returned no results, it returnsNone -
check_password_hash()hashes the submitted password and securely compares it to the stored hash -
sessionis a dict that stores data across requests. When validation succeeds, the user’sidis stored in a new session. The data is stored in a cookie that is sent to the browser
Load Logged In User
At the beginning of each request, if a user is logged in their information should be loaded and made available to other views:flaskr/auth.py
bp.before_app_request() registers a function that runs before the view function, no matter what URL is requested. It checks if a user id is stored in the session and gets that user’s data from the database, storing it on g.user.
The Logout View
To log out, remove the user id from the session:flaskr/auth.py
Require Authentication in Other Views
Creating, editing, and deleting blog posts will require a user to be logged in. A decorator can be used to check this for each view:flaskr/auth.py
Endpoints and URLs
Theurl_for() function generates the URL to a view based on a name and arguments. The name associated with a view is called the endpoint, and by default it’s the same as the name of the view function.
For example, the hello() view added to the app factory earlier has the name 'hello' and can be linked to with url_for('hello').
When using a blueprint, the name of the blueprint is prepended to the name of the function, so the endpoint for the login function is 'auth.login'.
Complete Auth Module
Here’s the completeflaskr/auth.py file:
flaskr/auth.py
