fix login_manager bug, use get_or_none

main
lza_menace 3 years ago
parent 4c17e2a5b0
commit 3d85028b29

@ -36,7 +36,7 @@ def create_app():
@login_manager.user_loader @login_manager.user_loader
def load_user(user_id): def load_user(user_id):
from xmrbackers.models import User from xmrbackers.models import User
user = User.get(user_id) user = User.get_or_none(user_id)
return user return user
return app return app

@ -1,15 +1,9 @@
import quart.flask_patch # import quart.flask_patch
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, BooleanField from wtforms import StringField
from wtforms.validators import DataRequired 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"}) 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"}) password = StringField('Password:', validators=[DataRequired()], render_kw={"placeholder": "Password", "class": "form-control", "type": "password"})

@ -17,8 +17,7 @@ class User(pw.Model):
register_date = pw.DateTimeField(default=datetime.now) register_date = pw.DateTimeField(default=datetime.now)
last_login_date = pw.DateTimeField(default=datetime.now) last_login_date = pw.DateTimeField(default=datetime.now)
username = pw.CharField(unique=True) username = pw.CharField(unique=True)
email = pw.CharField(unique=True) password = pw.CharField()
password = pw.CharField(unique=True)
@property @property
def is_authenticated(self): def is_authenticated(self):
@ -48,6 +47,9 @@ class CreatorProfile(pw.Model):
create_date = pw.DateTimeField(default=datetime.now) create_date = pw.DateTimeField(default=datetime.now)
last_login_date = pw.DateTimeField(default=datetime.now) last_login_date = pw.DateTimeField(default=datetime.now)
wallet_address = pw.CharField(null=True) 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() bio = pw.CharField()
class Meta: class Meta:
@ -58,6 +60,7 @@ class BackerProfile(pw.Model):
id = pw.AutoField() id = pw.AutoField()
register_date = pw.DateTimeField(default=datetime.now) register_date = pw.DateTimeField(default=datetime.now)
last_login_date = pw.DateTimeField(default=datetime.now) last_login_date = pw.DateTimeField(default=datetime.now)
email = pw.CharField(unique=True, null=True)
class Meta: class Meta:
database = db database = db

@ -4,7 +4,7 @@ from quart import flash, redirect, url_for
from flask_login import login_user, logout_user, current_user from flask_login import login_user, logout_user, current_user
from xmrbackers.factory import bcrypt from xmrbackers.factory import bcrypt
from xmrbackers.forms import Register, Login from xmrbackers.forms import UserAuth
from xmrbackers.models import User from xmrbackers.models import User
@ -12,23 +12,22 @@ bp = Blueprint('auth', 'auth')
@bp.route("/register", methods=["GET", "POST"]) @bp.route("/register", methods=["GET", "POST"])
async def register(): async def register():
form = Register() form = UserAuth()
if current_user.is_authenticated: if current_user.is_authenticated:
await flash('Already registered and authenticated.') await flash('Already registered and authenticated.')
return redirect(url_for('meta.index')) return redirect(url_for('meta.index'))
if form.validate_on_submit(): if form.validate_on_submit():
# Check if email already exists # Check if username already exists
user = User.select().where( user = User.select().where(
User.email == form.email.data User.username == form.username.data
).first() ).first()
if user: if user:
await flash('This email is already registered.') await flash('This username is already registered.')
return redirect(url_for('auth.login')) return redirect(url_for('auth.login'))
# Save new user # Save new user
user = User( user = User(
email=form.email.data,
username=form.username.data, username=form.username.data,
password=bcrypt.generate_password_hash(form.password.data).decode('utf8'), password=bcrypt.generate_password_hash(form.password.data).decode('utf8'),
) )
@ -40,7 +39,7 @@ async def register():
@bp.route("/login", methods=["GET", "POST"]) @bp.route("/login", methods=["GET", "POST"])
async def login(): async def login():
form = Login() form = UserAuth()
if current_user.is_authenticated: if current_user.is_authenticated:
await flash('Already logged in.') await flash('Already logged in.')
return redirect(url_for('meta.index')) return redirect(url_for('meta.index'))
@ -48,7 +47,7 @@ async def login():
if form.validate_on_submit(): if form.validate_on_submit():
# Check if user doesn't exist # Check if user doesn't exist
user = User.select().where( user = User.select().where(
User.email == form.email.data User.username == form.username.data
).first() ).first()
if not user: if not user:
await flash('Invalid username or password.') await flash('Invalid username or password.')

@ -34,7 +34,6 @@
</header> </header>
<span class="image"><img src="/static/images/monero-logo.png" width=150px></span> <span class="image"><img src="/static/images/monero-logo.png" width=150px></span>
</div> </div>
<a href="#swap" class="goto-next scrolly">Next</a>
</section> </section>
{% include 'includes/footer.html' %} {% include 'includes/footer.html' %}

@ -34,7 +34,6 @@
</header> </header>
<span class="image"><img src="/static/images/monero-logo.png" width=150px></span> <span class="image"><img src="/static/images/monero-logo.png" width=150px></span>
</div> </div>
<a href="#swap" class="goto-next scrolly">Next</a>
</section> </section>
{% include 'includes/footer.html' %} {% include 'includes/footer.html' %}

Loading…
Cancel
Save