|
|
|
from flask import 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 application.models.usermodel import User
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
return abort(400)
|
|
|
|
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("user/login.html", loginform=loginform)
|