0Pricing
Flask Academy · Lesson

User Loader and the UserMixin

Configure the LoginManager and user model.

User Loader and the UserMixin is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Meet Flask-Login

The Flask-Login extension manages who is logged in for you. It tracks the current user across requests so you do not wire sessions by hand.

pip install flask-login

Create a LoginManager

Everything starts with a LoginManager. You create one instance and connect it to your app so Flask-Login can hook into requests.

from flask_login import LoginManager
login_manager = LoginManager()
login_manager.init_app(app)

It Needs Two Things

To work, the manager needs a user model that follows its rules and a function that loads a user by id. Today you set up both.

The UserMixin Shortcut

Your model must expose a few properties like is_authenticated. The UserMixin class supplies them all, so you just inherit from it.

from flask_login import UserMixin
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)

What UserMixin Gives You

UserMixin adds is_authenticated, is_active, is_anonymous, and get_id for free. That last one returns your user id as a string.

The user_loader Callback

Flask-Login stores only the user id in the session. It calls your user_loader on each request to turn that id back into a real user.

Register the Loader

Decorate a function with login_manager.user_loader. It receives the id from the session and must return a user object or None.

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

Ids Arrive as Strings

The id passed to your loader is always a string, since it came from a cookie. Cast it to int before querying an integer primary key.

return User.query.get(int(user_id))

Return None for Unknowns

If the id matches no row, your loader should return None. Flask-Login then treats the request as coming from an anonymous visitor.

Override get_id If Needed

If your primary key is not named id, override get_id so it returns the right value. Most apps with a plain id column never touch it.

def get_id(self):
    return str(self.uuid)

Pieces Now Connected

With the manager, the UserMixin model, and a user_loader in place, Flask-Login can identify the current user on every incoming request.

Quick Check

Think about what the session actually stores between requests.

Recap

You wired a LoginManager, mixed UserMixin into your model, and wrote a user_loader. Flask-Login can now rebuild the current user from a session. 👤

Frequently asked questions

Is the “User Loader and the UserMixin” lesson free?

Yes — the full text of “User Loader and the UserMixin” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “User Loader and the UserMixin”?

Configure the LoginManager and user model. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “User Loader and the UserMixin” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Hash Passwords, Never Store Plaintext
  2. User Loader and the UserMixin
  3. login_user, logout_user, and Sessions
  4. Protect Views with login_required
← Back to Flask Academy