🎓 A1Learner
🔍
0 of 50 lessons completed

A Simple Flask Login

A simple login remembers a name in the session. The name stays until logout clears it.

Make the app

This line imports the login tools together. The secret key lets Flask sign the session.

from flask import Flask, request, redirect, url_for, session, render_template_string

app = Flask(__name__)
app.secret_key = "dev-key"

Log in with a name

A POST stores the posted name in the session and redirects to hello. A GET shows the login form.

@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        session["user"] = request.form.get("name", "")
        return redirect(url_for("hello"))
    return render_template_string('<form action="/login" method="post"><input name="name"><button>Log in</button></form>')

Greet the stored name

The view reads the stored name from the session. Guest is the default when nobody logged in.

@app.route("/hello")
def hello():
    return "Hello, " + session.get("user", "Guest") + "!"

Log out

Logout removes the name from the session and redirects to hello.

@app.route("/logout")
def logout():
    session.pop("user", None)
    return redirect(url_for("hello"))

Log in, then log out

The test client logs in as Ana and prints the greeting. It then logs out and prints the guest greeting.

client = app.test_client()
print(client.post("/login", data={"name": "Ana"}, follow_redirects=True).data.decode())
print(client.get("/logout", follow_redirects=True).data.decode())

Example

from flask import Flask, request, redirect, url_for, session, render_template_string

app = Flask(__name__)
app.secret_key = "dev-key"

@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        session["user"] = request.form.get("name", "")
        return redirect(url_for("hello"))
    return render_template_string('<form action="/login" method="post"><input name="name"><button>Log in</button></form>')

@app.route("/hello")
def hello():
    return "Hello, " + session.get("user", "Guest") + "!"

@app.route("/logout")
def logout():
    session.pop("user", None)
    return redirect(url_for("hello"))

client = app.test_client()
print(client.post("/login", data={"name": "Ana"}, follow_redirects=True).data.decode())
print(client.get("/logout", follow_redirects=True).data.decode())

Key points

  • The login form posts a name.
  • The view stores the name in session["user"].
  • The hello page greets the stored name or Guest.
  • Logout clears the name with session.pop.

In this example, the login route stores the posted name in the session and redirects to hello. The hello route greets the stored name, or Guest when nobody logged in. Logout removes the name. The test client logs in as Ana, then logs out, and the prints show both greetings.

Note

This login has no password, so it is a first sketch only.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Where does the simple login keep the logged-in name?