How it works...

Once the Flask modules are imported, the login decorator function provides the main logic for handling user authentication. The Python standard library's @wraps() decorator call does the same thing we have done previously with the def wrapper() function, except we are utilizing the functools.wraps function provided by Flask. This is necessary because the login decorator wraps and replaces the original function; without a wrapper, that original data would be lost during the handover.

The decorated_function() takes any number of arguments, either as positional or keyword:value pairs. This function first checks to see whether the global object g.user is None, that is, a user not logged in. If this is the case, the user is automatically redirected to the login page. Because of how Flask works, each page URL is actually a function call; the function's name dictates the URI path (more information on Flask functionality is provided later).

If the user is already logged in, then the f() function (the original function called) is called with the arguments the login decorator received. Finally, the decorator function ends, returning logic control back to the original function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset