HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

12) Users and User Authentication Lesson

Make a Flask Authentication Blueprint

3 min to complete · By Brandon Gigous

Now it's your turn to create a Flask authentication blueprint! This lesson will apply all that you've learned in the last section about blueprints, so refer to it if needed. Along with fleshing out the blueprint itself, you'll make all the necessary files and folders.

Create a Subpackage

Create a subpackage called auth within the app package. This is where all your authentication-related view functions, forms, and other stuff will live.

Create the Blueprint

Create the Blueprint in __init__.py. This file will be short.

Create the View Functions

Create the view functions in views.py. Make view functions login() and register(), with routes of the same name. Have them render templates login.html and register.html, respectively. Make these empty template files in the folder templates/auth/. You don't have to implement any functionality yet.

Register the Blueprint

Finally, register the Blueprint with the application. In your call to register_blueprint(), use the url_prefix argument with value '/auth'. This makes it so that any view functions associated with the blueprint will be accessible as /auth/login, for example.

Other Files

While you're at it, you can also create an empty forms.py.

Once you have all that, you're good to go for the next lesson, where you'll give your User model the functionality needed to see if a user is authenticated or not.

Summary: How to Create a Flask Authentication Blueprint

  1. Create a subpackage called auth within the app package
  2. Create the Blueprint in __init__.py.
  3. Create the view functions in views.py
  4. Finally, register the Blueprint with the application.