Flask-login doesn't actually have a user backend, it just handles the session machinery to help you login and logout users. You have to tell it (by decorating methods), what represents a user and it is also up to you to figure out how to know if a user is "active" or not (since being "active" can mean different things in different applications).

You should read the documentation and be sure what it does and does not do. Here I am only going to concentrate on wiring it up with the db backend.

To start off with, define a user object; which represents properties for your users. This object can then query databases, or LDAP, or whatever and it is the hook that connects the login mechanism with your database backend.

I will be using the login example script for this purpose.

class User(UserMixin):
    def __init__(self, name, id, active=True):
        self.name = name
        self.id = id
        self.active = active

    def is_active(self):
        # Here you should write whatever the code is
        # that checks the database if your user is active
        return self.active

    def is_anonymous(self):
        return False

    def is_authenticated(self):
        return True

Once you have the user object created, you need to write a method that loads the user (basically, creates an instance of the User class from above). This method is called with the user id.

@login_manager.user_loader
def load_user(id):
     # 1. Fetch against the database a user by `id` 
     # 2. Create a new object of `User` class and return it.
     u = DBUsers.query.get(id)
    return User(u.name,u.id,u.active)

Once you have these steps, your login method does this:

  1. Checks to see if the username and password match (against your database) - you need to write this code yourself.

  2. If authentication was successful you should pass an instance of the user to login_user()

Answer from Burhan Khalid on Stack Overflow
🌐
Readthedocs
flask-login.readthedocs.io
Flask-Login 0.7.0 documentation
Store the active user’s ID in the Flask Session, and let you easily log them in and out. Let you restrict views to logged-in (or logged-out) users. (login_required)
0.6.3
Once the actual application object has been created, you can configure it for login with: ... By default, Flask-Login uses sessions for authentication. This means you must set the secret key on your application, otherwise Flask will give you an error message telling you to do so.
0.4.1
Store the active user’s ID in the Flask Session, and let you easily log them in and out. Let you restrict views to logged-in (or logged-out) users. (login_required)
🌐
PyPI
pypi.org › project › Flask-Login
Flask-Login · PyPI
Flask-Login provides user session management for Flask.
      » pip install Flask-Login
    
Published   Oct 30, 2023
Version   0.6.3
Discussions

python - flask-login: can't understand how it works - Stack Overflow
I'm trying to understand how Flask-Login works. I see in their documentation that they use a pre-populated list of users. I want to play with a database-stored users list. However, I don't unders... More on stackoverflow.com
🌐 stackoverflow.com
Losing context path on /login with Flask-OIDC
When I try this in production, ...m/someapp/login ... proxy_set_header X-Forwarded-For $http_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_ignore_client_abort on; proxy_no_cache 1; proxy_cache_bypass 1; ... Create your account and connect with a world of communities. ... By continuing, you agree to our User Agreement and acknowledge that you understand the Privacy Policy. ... Accessibility Reddit, ... More on reddit.com
🌐 r/flask
2
2
October 16, 2024
How to create flask REST API
There really is not a "proper" way. If Jsonify works for you, then use it. That is what I typically use. Here is another option (if you are using sqlalchemy): https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xxiii-application-programming-interfaces-apis More on reddit.com
🌐 r/flask
20
13
July 18, 2024
(Help) flask-sqlalchmey error when I try to install it
What version of python are you using? More on reddit.com
🌐 r/learnpython
6
3
July 2, 2024
🌐
GitHub
github.com › maxcountryman › flask-login
GitHub - maxcountryman/flask-login: Flask user session management. · GitHub
Flask-Login provides user session management for Flask.
Starred by 3.7K users
Forked by 810 users
Languages   Python
🌐
Miguel Grinberg
blog.miguelgrinberg.com › post › the-flask-mega-tutorial-part-v-user-logins
The Flask Mega-Tutorial, Part V: User Logins - miguelgrinberg.com
Because Flask-Login knows nothing about databases, it needs the application's help in loading a user. For that reason, the extension expects that the application will configure a user loader function, that can be called to load a user given the ID.
🌐
InterSystems
community.intersystems.com › post › flask-and-flask-login-guide-building-secure-web-applications
Flask and Flask-Login: A Guide to Building Secure Web Applications | IDC
January 9, 2024 - Flask-Login simplifies the process of adding user authentication to a Flask application, making it easy to protect your application's resources from unauthorized access. It provides user session management for Flask.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-add-authentication-to-your-app-with-flask-login
Add Authentication to Flask Apps with Flask-Login | DigitalOcean
October 22, 2025 - Learn how to add secure authentication to your Flask app using Flask-Login. Implement user sessions, login pages, and access control with Python.
Find elsewhere
Top answer
1 of 7
88

Flask-login doesn't actually have a user backend, it just handles the session machinery to help you login and logout users. You have to tell it (by decorating methods), what represents a user and it is also up to you to figure out how to know if a user is "active" or not (since being "active" can mean different things in different applications).

You should read the documentation and be sure what it does and does not do. Here I am only going to concentrate on wiring it up with the db backend.

To start off with, define a user object; which represents properties for your users. This object can then query databases, or LDAP, or whatever and it is the hook that connects the login mechanism with your database backend.

I will be using the login example script for this purpose.

class User(UserMixin):
    def __init__(self, name, id, active=True):
        self.name = name
        self.id = id
        self.active = active

    def is_active(self):
        # Here you should write whatever the code is
        # that checks the database if your user is active
        return self.active

    def is_anonymous(self):
        return False

    def is_authenticated(self):
        return True

Once you have the user object created, you need to write a method that loads the user (basically, creates an instance of the User class from above). This method is called with the user id.

@login_manager.user_loader
def load_user(id):
     # 1. Fetch against the database a user by `id` 
     # 2. Create a new object of `User` class and return it.
     u = DBUsers.query.get(id)
    return User(u.name,u.id,u.active)

Once you have these steps, your login method does this:

  1. Checks to see if the username and password match (against your database) - you need to write this code yourself.

  2. If authentication was successful you should pass an instance of the user to login_user()

2 of 7
19

Flask-login will try and load a user BEFORE every request. So yes, your example code below will be called before every request. It is used to check what userid is in the current session and will load the user object for that id.

@login_manager.user_loader
def load_user(userid):
    #print 'this is executed',userid
    return user(userid, 'asdf')        

If you look at the Flask-login source code on github, there is a line under function init_app which goes:

app.before_request(self._load_user)

So before every request, the _load_user function is called. The _load_user functions actually calls another function "reload_user()" based on conditions. And finally, reload_user() function calls your callback function that you wrote (load_user() in your example).

Also, flask-login only provides the mechanism to login/logout a user. It does not care if you are using mysql database.

🌐
Flask
flask.palletsprojects.com
Welcome to Flask — Flask Documentation (3.1.x)
Flask provides configuration and conventions, with sensible defaults, to get started. This section of the documentation explains the different parts of the Flask framework and how they can be used, customized, and extended.
🌐
Auth0
auth0.com › quickstarts › regular web app › add login to your flask application
Add Login to Your Flask Application - Auth0 Docs
June 24, 2026 - This quickstart demonstrates how to add Auth0 authentication to a Flask application. You’ll build a secure web application with login functionality, protected routes, and user profile access using the Auth0 WebApp Python SDK.
🌐
Medium
medium.com › @icodewithben › creating-a-simple-flask-login-website-378e4f7f3b4c
Creating a simple Flask login website | by Icodewithben | Medium
January 31, 2024 - Creating a simple Flask login website Follow this series of 3 lessons (link to next lesson at the end of the article) to understand the basics of flask and how to setup simple routes, create an sql …
🌐
Flash Pack
flashpack.com › us
Flash Pack | Social Adventures for like-minded solo travellers
November 9, 2023 - We unite like-minded people in their 30s, 40s & 50s on group adventures where 90% travel solo. Book your next trip with Flash Pack today.
🌐
Medium
medium.com › @mathur.danduprolu › user-authentication-and-authorization-in-flask-building-secure-login-and-access-control-part-5-7-59679a08cdc3
User Authentication and Authorization in Flask: Building Secure Login and Access Control [Part 5/7] | by Mathur Danduprolu | Medium
November 13, 2024 - These templates provide basic forms for user registration, login, and a protected dashboard page. You’ve just implemented user authentication and authorization in Flask using Flask-Login.
🌐
DEV Community
dev.to › imdhruv99 › flask-login-register-logout-implementation-3hep
Flask Login-Register-Logout Implementation - DEV Community
September 15, 2020 - This is the Part-2 of the Flask Series. I have explained What is Rest API and Implementation in Flask. Please refer that article first for better understanding of rest apis. Today we are going to implement Login-Register Flow in Flask.So without any further let's jump into it.
🌐
Plain English
python.plainenglish.io › flask-login-authentication-system-b5b32bdcd67a
Flask Login Authentication System | Python in Plain English
November 18, 2024 - Let’s set up the /login route to render the login form and handle user authentication: from flask import Flask, render_template, redirect, url_for, flash, request from flask_login import LoginManager, login_user, login_required, logout_user, current_user from flask_bcrypt import Bcrypt from werkzeug.security import check_password_hash app = Flask(__name__) app.secret_key = 'your_secret_key' # Needed for session management bcrypt = Bcrypt(app) login_manager = LoginManager(app) login_manager.login_view = 'login' @login_manager.user_loader def load_user(user_id): return get_user_by_username(use
🌐
Desarrollolibre
desarrollolibre.net › blog › flask › how-to-add-authentication-with-flask-login
How to implement user authentication with Flask-Login step by step - Desarrollolibre
To use Flask-Login, we create a model for the user that defines the methods and parameters required by this extension, such as is_authenticated, is_active, and is_anonymous, and then initialize the extension in your Flask application.
Published   October 30, 2025
🌐
Flask
flask.palletsprojects.com › en › stable › quickstart
Quickstart — Flask Documentation (3.1.x)
First of all you have to import it from the flask module: ... The current request method is available by using the method attribute. To access form data (data transmitted in a POST or PUT request) you can use the form attribute. Here is a full example of the two attributes mentioned above: @app.route('/login', methods=['POST', 'GET']) def login(): error = None if request.method == 'POST': if valid_login(request.form['username'], request.form['password']): return log_the_user_in(request.form['username']) else: error = 'Invalid username/password' # the code below is executed if the request method # was GET or the credentials were invalid return render_template('login.html', error=error)
🌐
Real Python
realpython.com › flask-google-login
Create a Flask Application With Google Login – Real Python
January 25, 2023 - In this step-by-step tutorial, you'll create a Flask application that lets users sign in using their Google login. You'll learn about OAuth 2 and OpenID Connect and also find out how to implement some code to handle user session management.
🌐
Hackers and Slackers
hackersandslackers.com › flask-login-user-authentication
Handle User Accounts & Authentication in Flask with Flask-Login
April 4, 2019 - Flask-Login is a dope library that handles all aspects of user management, including user signups, encrypting passwords, managing sessions, and securing parts of our app behind login walls.