From 3d85028b293f9e0b8a9e3f7b28072492cf2f24ae Mon Sep 17 00:00:00 2001 From: lza_menace Date: Thu, 1 Jul 2021 22:48:40 -0700 Subject: [PATCH] fix login_manager bug, use get_or_none --- xmrbackers/factory.py | 2 +- xmrbackers/forms.py | 12 +++--------- xmrbackers/models.py | 7 +++++-- xmrbackers/routes/auth.py | 15 +++++++-------- xmrbackers/templates/auth/login.html | 1 - xmrbackers/templates/auth/register.html | 1 - 6 files changed, 16 insertions(+), 22 deletions(-) diff --git a/xmrbackers/factory.py b/xmrbackers/factory.py index 3e02d3d..bb4e902 100644 --- a/xmrbackers/factory.py +++ b/xmrbackers/factory.py @@ -36,7 +36,7 @@ def create_app(): @login_manager.user_loader def load_user(user_id): from xmrbackers.models import User - user = User.get(user_id) + user = User.get_or_none(user_id) return user return app diff --git a/xmrbackers/forms.py b/xmrbackers/forms.py index 657e331..cf300fe 100644 --- a/xmrbackers/forms.py +++ b/xmrbackers/forms.py @@ -1,15 +1,9 @@ -import quart.flask_patch +# import quart.flask_patch from flask_wtf import FlaskForm -from wtforms import StringField, BooleanField +from wtforms import StringField from wtforms.validators import DataRequired -class Login(FlaskForm): +class UserAuth(FlaskForm): username = StringField('Username:', validators=[DataRequired()], render_kw={"placeholder": "Username", "class": "form-control", "type": "text"}) - email = StringField('Email Address:', validators=[DataRequired()], render_kw={"placeholder": "Email", "class": "form-control", "type": "email"}) - password = StringField('Password:', validators=[DataRequired()], render_kw={"placeholder": "Password", "class": "form-control", "type": "password"}) - -class Register(FlaskForm): - username = StringField('Username:', validators=[DataRequired()], render_kw={"placeholder": "Username", "class": "form-control", "type": "text"}) - email = StringField('Email Address:', validators=[DataRequired()], render_kw={"placeholder": "Email", "class": "form-control", "type": "email"}) password = StringField('Password:', validators=[DataRequired()], render_kw={"placeholder": "Password", "class": "form-control", "type": "password"}) diff --git a/xmrbackers/models.py b/xmrbackers/models.py index 43b52f9..6477dd9 100644 --- a/xmrbackers/models.py +++ b/xmrbackers/models.py @@ -17,8 +17,7 @@ class User(pw.Model): register_date = pw.DateTimeField(default=datetime.now) last_login_date = pw.DateTimeField(default=datetime.now) username = pw.CharField(unique=True) - email = pw.CharField(unique=True) - password = pw.CharField(unique=True) + password = pw.CharField() @property def is_authenticated(self): @@ -48,6 +47,9 @@ class CreatorProfile(pw.Model): create_date = pw.DateTimeField(default=datetime.now) last_login_date = pw.DateTimeField(default=datetime.now) wallet_address = pw.CharField(null=True) + website = pw.CharField(null=True) + twitter_handle = pw.CharField(null=True) + email = pw.CharField(unique=True, null=True) bio = pw.CharField() class Meta: @@ -58,6 +60,7 @@ class BackerProfile(pw.Model): id = pw.AutoField() register_date = pw.DateTimeField(default=datetime.now) last_login_date = pw.DateTimeField(default=datetime.now) + email = pw.CharField(unique=True, null=True) class Meta: database = db diff --git a/xmrbackers/routes/auth.py b/xmrbackers/routes/auth.py index da27e79..3e34cec 100644 --- a/xmrbackers/routes/auth.py +++ b/xmrbackers/routes/auth.py @@ -4,7 +4,7 @@ from quart import flash, redirect, url_for from flask_login import login_user, logout_user, current_user from xmrbackers.factory import bcrypt -from xmrbackers.forms import Register, Login +from xmrbackers.forms import UserAuth from xmrbackers.models import User @@ -12,23 +12,22 @@ bp = Blueprint('auth', 'auth') @bp.route("/register", methods=["GET", "POST"]) async def register(): - form = Register() + form = UserAuth() if current_user.is_authenticated: await flash('Already registered and authenticated.') return redirect(url_for('meta.index')) if form.validate_on_submit(): - # Check if email already exists + # Check if username already exists user = User.select().where( - User.email == form.email.data + User.username == form.username.data ).first() if user: - await flash('This email is already registered.') + await flash('This username is already registered.') return redirect(url_for('auth.login')) # Save new user user = User( - email=form.email.data, username=form.username.data, password=bcrypt.generate_password_hash(form.password.data).decode('utf8'), ) @@ -40,7 +39,7 @@ async def register(): @bp.route("/login", methods=["GET", "POST"]) async def login(): - form = Login() + form = UserAuth() if current_user.is_authenticated: await flash('Already logged in.') return redirect(url_for('meta.index')) @@ -48,7 +47,7 @@ async def login(): if form.validate_on_submit(): # Check if user doesn't exist user = User.select().where( - User.email == form.email.data + User.username == form.username.data ).first() if not user: await flash('Invalid username or password.') diff --git a/xmrbackers/templates/auth/login.html b/xmrbackers/templates/auth/login.html index 6543ed5..40af071 100644 --- a/xmrbackers/templates/auth/login.html +++ b/xmrbackers/templates/auth/login.html @@ -34,7 +34,6 @@ - Next {% include 'includes/footer.html' %} diff --git a/xmrbackers/templates/auth/register.html b/xmrbackers/templates/auth/register.html index fa0af08..7c167c5 100644 --- a/xmrbackers/templates/auth/register.html +++ b/xmrbackers/templates/auth/register.html @@ -34,7 +34,6 @@ - Next {% include 'includes/footer.html' %}