When I started off to code for my new project which I had named as Emolytics, I had some small starting troubles with respect to using those features in flask which I had not exposure with. I had give some dedicated time in getting to understand these things. Here I am trying to pen down some points which I felt should be read by anyone who is starting to code with flask.
Now I’ll start off this with Flask-Login module, an extension in Flask which provides great help in managing users and their sessions.
Flask-Login module is to be installed:
Next we can go directly into the coding section. Along with the Flask application initialisation, a LoginManager object is also to be created.
A login page:
This template page will help you to show the user a login page and collect the user credentials and later we can process it to verify wheather the user has registered or is a valid user. A part of login.html will be like:
End point function for login:
Corresponding login function is also defined under the decorator app.route. This login function is going to be used later to find the original login endpoint to which all the anonymous users will be redirected to when they try to access any restricted pages.
For this functionality we have to assign the function name to the login_manager
variable login_view like:
login_manager.login_view = 'login'. Whenever access
to a login_required page happens it is redirected to this login page with next
url as the url currently accessed.
Below a basic login function is shown for reference purpose:
Function to load the user:
Before every request the variable current_user is populated. This loading is done using many hooks which can be defined. Some of the ways are mentioned below:
From user_id attribute in session variable: Once the login is completed, a key user_id can be put in the session dictionary, which will be taken by this hook to load a user before all the requests.
From the request arriving: The incomming requests will be given as the arguement to this hook and after processing it should return the user object.
There are many more different hooks. You can just look in to flask.ext.login help page.
The decorator login_required will manupulate some class variables to keep track of logged in users, anonymous users and similar things. So its better that either we have them put explicitly into the User model class or use the UserMixin class which will have all the necessarry data variables that was mentioned earlier.
One major variable that is be present should be is_authenticated, which will be set when the user validation is successful and will be unset when user logs out. These events happen when the functions login_user and logout_user are called.