Skip to content

Instantly share code, notes, and snippets.

@gergob gergob/login.py
Last active Aug 29, 2015

Embed
What would you like to do?
@app.route("/")
@user_login_needed
def index():
return render_template("index.html")
return user_login_needed(index) # no extra parameters passed since index does not take any parameters
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "GET":
return render_template("login.html")
elif request.method == "POST":
user_name = request.form['username']
password = request.form['password']
if is_user_valid(user_name, password):
session['user_logged_in'] = True
error_message = "User {} successfuly logged in.".format(user_name)
flash(error_message)
print(error_message)
return redirect(url_for("index"))
else:
print("Invalid user.")
flash("Invalid user/password!")
return redirect(url_for("login"))
else:
error_message = "Invalid request method:{}".format(request.method)
print(error_message)
flash(error_message)
return redirect(url_for("login"))
def is_user_valid(user_name, password):
return user_name == "john" and password == "1234"
@app.route("/logout", methods=["GET"])
def logout():
session['user_logged_in'] = False
print("User logout successful.")
flash("Logout successful")
return redirect(url_for("login"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.