diff --git a/app/__init__.py b/app/__init__.py index f057191c..14eb1bd2 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -23,7 +23,7 @@ from flask_github import GitHub from flask_wtf.csrf import CSRFProtect from flask_flatpages import FlatPages from flask_babel import Babel -from flask_login import logout_user, current_user +from flask_login import logout_user, current_user, LoginManager import os, redis app = Flask(__name__, static_folder="public/static") @@ -48,6 +48,10 @@ gravatar = Gravatar(app, use_ssl=True, base_url=None) +login_manager = LoginManager() +login_manager.init_app(app) +login_manager.login_view = "users.login" + from .sass import sass sass(app) @@ -64,7 +68,13 @@ init_app(app) # def get_locale(): # return request.accept_languages.best_match(app.config['LANGUAGES'].keys()) -from . import models, tasks, template_filters, usermgr +from . import models, tasks, template_filters + +@login_manager.user_loader +def load_user(user_id): + return models.User.query.filter_by(username=user_id).first() + + from .blueprints import create_blueprints create_blueprints(app) diff --git a/app/blueprints/users/account.py b/app/blueprints/users/account.py index 615d7949..4dd14dc0 100644 --- a/app/blueprints/users/account.py +++ b/app/blueprints/users/account.py @@ -36,16 +36,15 @@ class LoginForm(FlaskForm): def handle_login(form): - username = form.username.data.strip() - user = User.query.filter(or_(User.username == username, User.email == username)).first() - - def show_safe_err(err): if "@" in username: flash("Incorrect email or password", "danger") else: - flash(err, "error") + flash(err, "danger") + + username = form.username.data.strip() + user = User.query.filter(or_(User.username == username, User.email == username)).first() if user is None: return show_safe_err("User {} does not exist".format(username)) @@ -67,7 +66,6 @@ def handle_login(form): return redirect(next or url_for("homepage.home")) - @bp.route("/user/login/", methods=["GET", "POST"]) def login(): form = LoginForm(request.form) @@ -117,8 +115,16 @@ def register(): return render_template("users/register.html", form=form) +class ForgotPassword(FlaskForm): + email = StringField("Email", [InputRequired(), Email()]) + submit = SubmitField("Reset Password") + @bp.route("/user/forgot-password/", methods=["GET", "POST"]) def forgot_password(): + form = ForgotPassword(request.form) + if form.validate_on_submit(): + pass + return "Forgot password page" @@ -128,7 +134,6 @@ class SetPasswordForm(FlaskForm): password2 = PasswordField("Verify password", [InputRequired(), Length(8, 100)]) submit = SubmitField("Save") - @bp.route("/user/change-password/", methods=["GET", "POST"]) @login_required def change_password(): @@ -180,7 +185,7 @@ def set_password(): return render_template("users/set_password.html", form=form, optional=request.args.get("optional")) -@bp.route("/users/verify/") +@bp.route("/user/verify/") def verify_email(): token = request.args.get("token") ver = UserEmailVerification.query.filter_by(token=token).first() diff --git a/app/models.py b/app/models.py index cd49588b..a4fac5da 100644 --- a/app/models.py +++ b/app/models.py @@ -20,13 +20,12 @@ import enum from urllib.parse import urlparse from flask import url_for +from flask_login import UserMixin from flask_migrate import Migrate from flask_sqlalchemy import SQLAlchemy, BaseQuery from sqlalchemy_searchable import SearchQueryMixin, make_searchable from sqlalchemy_utils.types import TSVectorType - -from .usermgr import UserMixin, login_manager -from . import app, gravatar +from . import app, gravatar, login_manager # Initialise database db = SQLAlchemy(app) @@ -1481,7 +1480,3 @@ if app.config.get("LOG_SQL"): import logging logging.basicConfig() logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) - -@login_manager.user_loader -def load_user(user_id): - return User.query.filter_by(username=user_id).first() diff --git a/app/usermgr.py b/app/usermgr.py deleted file mode 100644 index 06d0f70c..00000000 --- a/app/usermgr.py +++ /dev/null @@ -1,10 +0,0 @@ -from . import app -from flask_login import LoginManager - -login_manager = LoginManager() -login_manager.init_app(app) -login_manager.login_view = "users.login" - -class UserMixin: - is_authenticated = True - is_anonymous = False