|
|
|
from flask import (
|
|
|
|
Blueprint,
|
|
|
|
abort,
|
|
|
|
flash,
|
|
|
|
redirect,
|
|
|
|
render_template,
|
|
|
|
request,
|
|
|
|
url_for,
|
|
|
|
)
|
|
|
|
from flask_bcrypt import check_password_hash
|
|
|
|
from flask_login import login_user
|
|
|
|
|
|
|
|
from forms.loginform import LoginForm
|
|
|
|
from models.user_model import User
|
|
|
|
|
|
|
|
login_section = Blueprint(
|
|
|
|
"login",
|
|
|
|
__name__,
|
|
|
|
template_folder="templates/statuspengguna",
|
|
|
|
static_folder="static",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@login_section.route("/", methods=["GET", "POST"])
|
|
|
|
def login():
|
|
|
|
return LoginUser()
|
|
|
|
|
|
|
|
|
|
|
|
def LoginUser():
|
|
|
|
loginform = LoginForm()
|
|
|
|
if loginform.validate_on_submit():
|
|
|
|
try:
|
|
|
|
user = User.query.filter_by(email=loginform.email.data).first()
|
|
|
|
if user is None:
|
|
|
|
loginform.password.errors.append("Invalid email or password!")
|
|
|
|
return render_template("login.html", loginform=loginform)
|
|
|
|
if check_password_hash(user.password, loginform.password.data):
|
|
|
|
login_user(user)
|
|
|
|
flash("Logged in successfully.", "success")
|
|
|
|
next = request.args.get("next")
|
|
|
|
if next is not None and not is_safe_url(next): # noqa: F821
|
|
|
|
print(next)
|
|
|
|
return abort(400)
|
|
|
|
print("index")
|
|
|
|
return redirect(next or url_for("index"))
|
|
|
|
else:
|
|
|
|
flash("Invalid email or password!", "danger")
|
|
|
|
loginform.password.errors.append("Invalid email or password!")
|
|
|
|
return render_template("login.html", loginform=loginform)
|
|
|
|
except Exception as e:
|
|
|
|
flash(e, "danger")
|
|
|
|
return render_template("login.html", loginform=loginform)
|