from flask import ( render_template, redirect, request, flash, url_for, abort, ) from usermodel import User from forms.loginform import LoginForm from flask_login import login_user from flask_bcrypt import check_password_hash 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)