You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
1.3 KiB
30 lines
1.3 KiB
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 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("login.html", loginform=loginform)
|
|
|