0Pricing
Flask Academy · Lesson

login_user, logout_user, and Sessions

Sign users in and out of a session.

login_user, logout_user, and Sessions is a free Flask Academy lesson on CoddyKit — lesson 3 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.

Logging Someone In

Once a password checks out, you tell Flask-Login the user is signed in by calling login_user. That single call sets up the session for you.

from flask_login import login_user
login_user(user)

A Typical Login Route

In a login view you find the user, verify the password, then call login_user. After that, redirect them to a real page like the dashboard.

if user and user.check_password(pw):
    login_user(user)
    return redirect(url_for("dashboard"))

What login_user Stores

login_user does not save the whole object. It writes the user id into the session cookie, and your user_loader rebuilds the user later.

Remember Me

Pass remember=True to keep the user logged in after the browser closes. Flask-Login sets a long-lived cookie instead of a session-only one.

login_user(user, remember=True)

Accessing current_user

Anywhere in a view you can read current_user to get the logged-in person. It is a proxy that always points at the active request's user.

from flask_login import current_user
name = current_user.username

Anonymous Users

If nobody is logged in, current_user is an anonymous user object. Its is_authenticated is False, so you can branch on that safely.

if current_user.is_authenticated:
    show_dashboard()

Logging Out

To sign a user out, call logout_user. It clears their id from the session so the next request treats them as anonymous again.

from flask_login import logout_user
logout_user()

A Logout Route

A logout view is tiny: call logout_user, then redirect home. There is no password to check, only the session to clear.

@app.route("/logout")
def logout():
    logout_user()
    return redirect(url_for("index"))

Secret Key Required

Sessions ride in a signed cookie, so your app needs a SECRET_KEY. Without it, login_user raises an error and nothing works.

app.config["SECRET_KEY"] = "a-long-random-value"

Sessions Are Per-User

Each browser gets its own signed cookie, so two users never see each other's session. Flask-Login keeps them cleanly separated.

Greeting the User

In templates you can use current_user too, since Flask-Login injects it. That makes a personalized navbar a one-line job.

<p>Hello, {{ current_user.username }}</p>

Quick Check

A user clicks Log out. Which call ends their session?

Recap

You now login_user after a password check, read current_user anywhere, and call logout_user to end a session. A SECRET_KEY ties it together. 🔑

Frequently asked questions

Is the “login_user, logout_user, and Sessions” lesson free?

Yes — the full text of “login_user, logout_user, and Sessions” 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 “login_user, logout_user, and Sessions”?

Sign users in and out of a session. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “login_user, logout_user, and Sessions” 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