When building a web application we might first start with the base of our application serving an endpoint saying “Hello World”.
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello Authentication World!'
The application we will be building will show how to use tools in Flask to authenticate users. The primary tool we can use to achieve our purposes of authenticating in Flask is Flask-Login.
Flask-Login is a third-party package that allows us to use pieces of code that enable us to perform authentication actions in our application.
We can manage user logins with the LoginManager
object from within Flask-Login, as shown below:
from flask_login import LoginManager login_manager = LoginManager()
LoginManager
is imported from theflask_login
package- a new
LoginManager
object namedlogin_manager
is created
Once a LoginManager
object is defined, we need to initialize the manager with our application. This can be done with the init_app()
method of a LoginManager
:
login_manager.init_app(app)
- our instance of
LoginManager
,login_manager
, calls itsinit_app()
method withapp
, an initialized Flask app, as an argument
Instructions
Import LoginManager
from flask_login
in app.py
Create an instance of LoginManager
named app_login_manager
.
Initialize your login_manager
with the app provided in app.py.