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()
LoginManageris imported from the
- a new
LoginManager object is defined, we need to initialize the manager with our application. This can be done with the
init_app() method of a
- our instance of
login_manager, calls its
app, an initialized Flask app, as an argument
flask_login in app.py
Create an instance of
login_manager with the app provided in app.py.