0Pricing
Flask Academy · บทเรียน

login_user, logout_user และเซสชัน

เข้าสู่ระบบและออกจากระบบผู้ใช้ในเซสชัน

login_user, logout_user และเซสชัน เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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. 🔑

คำถามที่พบบ่อย

บทเรียน “login_user, logout_user และเซสชัน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “login_user, logout_user และเซสชัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “login_user, logout_user และเซสชัน”

เข้าสู่ระบบและออกจากระบบผู้ใช้ในเซสชัน คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “login_user, logout_user และเซสชัน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม

ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แฮชรหัสผ่าน อย่าเก็บข้อความแบบไม่เข้ารหัส
  2. ตัวโหลดผู้ใช้และ UserMixin
  3. login_user, logout_user และเซสชัน
  4. ปกป้องมุมมองด้วย login_required
← กลับไปที่ Flask Academy