Skip to Content
Learn
Introduction to Authentication with Flask
Meet Flask-Login

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 the flask_login package
  • a new LoginManager object named login_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 its init_app() method with app, an initialized Flask app, as an argument

Instructions

1.

Import LoginManager from flask_login in app.py

2.

Create an instance of LoginManager named app_login_manager.

3.

Initialize your login_manager with the app provided in app.py.

Folder Icon

Sign up to start coding

Already have an account?