mirror of
https://github.com/Lynnesbian/FediBooks/
synced 2024-11-25 16:48:58 +00:00
implement errors that can be shown to the user
This commit is contained in:
parent
4f2741c384
commit
fb54d65d74
6 changed files with 53 additions and 12 deletions
|
@ -178,3 +178,11 @@ form .row {
|
||||||
background: center/contain url("https://lynnesbian.space/img/bune.png");
|
background: center/contain url("https://lynnesbian.space/img/bune.png");
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
background-color: #e66;
|
||||||
|
color: white;
|
||||||
|
text-align: center;
|
||||||
|
font-size: 1.6em;
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
<h1 class="thin centred">Add account</h1>
|
<h1 class="thin centred">Add account</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{%include 'error.html' %}
|
||||||
|
|
||||||
<div class="container centred">
|
<div class="container centred">
|
||||||
<form action="/bot/accounts/add" method="POST">
|
<form action="/bot/accounts/add" method="POST">
|
||||||
{% if session['step'] == 1 %}
|
{% if session['step'] == 1 %}
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
<h1 class="thin centred">Create bot</h1>
|
<h1 class="thin centred">Create bot</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{% include 'error.html' %}
|
||||||
|
|
||||||
<div class="container centred">
|
<div class="container centred">
|
||||||
<form action="/bot/create" method="POST">
|
<form action="/bot/create" method="POST">
|
||||||
{% if session['step'] == 1 %}
|
{% if session['step'] == 1 %}
|
||||||
|
|
3
templates/error.html
Normal file
3
templates/error.html
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{% if error != None %}
|
||||||
|
<div class="error"><i class="fas fa-exclamation-triangle"></i> {{ error }}</div>
|
||||||
|
{% endif %}
|
|
@ -11,6 +11,8 @@
|
||||||
<h1 class="thin centred">{% if signup %}Sign up{% else %}Log in{% endif %}</h1>
|
<h1 class="thin centred">{% if signup %}Sign up{% else %}Log in{% endif %}</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{% include 'error.html' %}
|
||||||
|
|
||||||
<div class="container centred">
|
<div class="container centred">
|
||||||
<form action="/do/{% if signup %}signup{% else %}login{% endif %}" method="POST">
|
<form action="/do/{% if signup %}signup{% else %}login{% endif %}" method="POST">
|
||||||
<p>
|
<p>
|
||||||
|
|
48
webui.py
48
webui.py
|
@ -60,11 +60,16 @@ def about():
|
||||||
|
|
||||||
@app.route("/login")
|
@app.route("/login")
|
||||||
def show_login_page():
|
def show_login_page():
|
||||||
return render_template("login.html", signup = False)
|
error = None
|
||||||
|
if 'error' in session:
|
||||||
|
error = session.pop('error')
|
||||||
|
return render_template("login.html", signup = False, error = error)
|
||||||
|
|
||||||
@app.route("/signup")
|
@app.route("/signup")
|
||||||
def show_signup_page(error = None):
|
def show_signup_page():
|
||||||
#TODO: display error if any
|
error = None
|
||||||
|
if 'error' in session:
|
||||||
|
error = session.pop('error')
|
||||||
return render_template("login.html", signup = True)
|
return render_template("login.html", signup = True)
|
||||||
|
|
||||||
@app.route("/settings")
|
@app.route("/settings")
|
||||||
|
@ -136,8 +141,13 @@ def bot_accounts(id):
|
||||||
|
|
||||||
@app.route("/bot/accounts/add", methods = ['GET', 'POST'])
|
@app.route("/bot/accounts/add", methods = ['GET', 'POST'])
|
||||||
def bot_accounts_add():
|
def bot_accounts_add():
|
||||||
|
error = None
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
if session['step'] == 1:
|
if session['step'] == 1:
|
||||||
|
if request.form['account'] == session['bot']:
|
||||||
|
error = "Bots cannot learn from themselves."
|
||||||
|
return render_template("bot_accounts_add.html", error)
|
||||||
|
|
||||||
# look up user
|
# look up user
|
||||||
handle_list = request.form['account'].split('@')
|
handle_list = request.form['account'].split('@')
|
||||||
username = handle_list[1]
|
username = handle_list[1]
|
||||||
|
@ -159,7 +169,8 @@ def bot_accounts_add():
|
||||||
found = True
|
found = True
|
||||||
break
|
break
|
||||||
if not found:
|
if not found:
|
||||||
return "Couldn't find a valid ActivityPub outbox URL."
|
error = "Couldn't find a valid ActivityPub outbox URL."
|
||||||
|
return render_template("bot_accounts_add.html", error = error)
|
||||||
|
|
||||||
# 3. format as outbox URL and check to make sure it works
|
# 3. format as outbox URL and check to make sure it works
|
||||||
outbox = "{}/outbox?page=true".format(uri)
|
outbox = "{}/outbox?page=true".format(uri)
|
||||||
|
@ -171,10 +182,13 @@ def bot_accounts_add():
|
||||||
c.execute("INSERT INTO `bot_learned_accounts` (`bot_id`, `fedi_id`) VALUES (%s, %s)", (session['bot'], request.form['account']))
|
c.execute("INSERT INTO `bot_learned_accounts` (`bot_id`, `fedi_id`) VALUES (%s, %s)", (session['bot'], request.form['account']))
|
||||||
c.close()
|
c.close()
|
||||||
mysql.connection.commit()
|
mysql.connection.commit()
|
||||||
|
else:
|
||||||
|
error = "Couldn't access ActivityPub outbox. {} may require authenticated fetches, which FediBooks doesn't support yet."
|
||||||
|
return render_template("bot_accounts_add.html", error = error)
|
||||||
|
|
||||||
return redirect("/bot/accounts/{}".format(session['bot']), 303)
|
return redirect("/bot/accounts/{}".format(session['bot']), 303)
|
||||||
|
|
||||||
return render_template("bot_accounts_add.html")
|
return render_template("bot_accounts_add.html", error = error)
|
||||||
|
|
||||||
@app.route("/bot/accounts/toggle/<id>")
|
@app.route("/bot/accounts/toggle/<id>")
|
||||||
def bot_accounts_toggle(id):
|
def bot_accounts_toggle(id):
|
||||||
|
@ -205,7 +219,7 @@ def bot_accounts_delete(id):
|
||||||
|
|
||||||
@app.route("/bot/create/", methods=['GET', 'POST'])
|
@app.route("/bot/create/", methods=['GET', 'POST'])
|
||||||
def bot_create():
|
def bot_create():
|
||||||
#TODO: error handling
|
error = None
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
if session['step'] == 1:
|
if session['step'] == 1:
|
||||||
# strip leading https://, if provided
|
# strip leading https://, if provided
|
||||||
|
@ -270,7 +284,7 @@ def bot_create():
|
||||||
del session['instance']
|
del session['instance']
|
||||||
del session['instance_type']
|
del session['instance_type']
|
||||||
session['step'] = 1
|
session['step'] = 1
|
||||||
return bot_create()
|
return redirect(url_for("bot_create"), 303)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if session['step'] == 4:
|
if session['step'] == 4:
|
||||||
|
@ -282,7 +296,8 @@ def bot_create():
|
||||||
handle = "@{}@{}".format(username, session['instance'])
|
handle = "@{}@{}".format(username, session['instance'])
|
||||||
except:
|
except:
|
||||||
# authentication error occurred
|
# authentication error occurred
|
||||||
return render_template("bot_oauth_error.html")
|
error = "Authentication failed."
|
||||||
|
return render_template("bot_create.html", error = error)
|
||||||
|
|
||||||
# authentication success!!
|
# authentication success!!
|
||||||
c = mysql.connection.cursor()
|
c = mysql.connection.cursor()
|
||||||
|
@ -301,7 +316,9 @@ def bot_create():
|
||||||
del session['client_id']
|
del session['client_id']
|
||||||
del session['client_secret']
|
del session['client_secret']
|
||||||
|
|
||||||
return render_template("bot_create.html")
|
if 'error' in session:
|
||||||
|
error = session.pop('error')
|
||||||
|
return render_template("bot_create.html", error = error)
|
||||||
|
|
||||||
@app.route("/bot/create/back")
|
@app.route("/bot/create/back")
|
||||||
def bot_create_back():
|
def bot_create_back():
|
||||||
|
@ -319,10 +336,12 @@ def do_signup():
|
||||||
# email validation is basically impossible without actually sending an email to the address
|
# email validation is basically impossible without actually sending an email to the address
|
||||||
# because fedibooks can't send email yet, we'll just check if the string contains an @ ;)
|
# because fedibooks can't send email yet, we'll just check if the string contains an @ ;)
|
||||||
if "@" not in request.form['email']:
|
if "@" not in request.form['email']:
|
||||||
return show_signup_page("Invalid email address.")
|
session['error'] = "Invalid email address."
|
||||||
|
return redirect(url_for("show_signup_page"), 303)
|
||||||
|
|
||||||
if len(request.form['password']) < 8:
|
if len(request.form['password']) < 8:
|
||||||
return show_signup_page("Password too short.")
|
session['error'] = "Password too short."
|
||||||
|
return redirect(url_for("show_signup_page"), 303)
|
||||||
|
|
||||||
pw_hashed = hashlib.sha256(request.form['password'].encode('utf-8')).digest()
|
pw_hashed = hashlib.sha256(request.form['password'].encode('utf-8')).digest()
|
||||||
pw = bcrypt.hashpw(pw_hashed, bcrypt.gensalt(12))
|
pw = bcrypt.hashpw(pw_hashed, bcrypt.gensalt(12))
|
||||||
|
@ -350,12 +369,17 @@ def do_login():
|
||||||
c.execute("SELECT * FROM users WHERE email = %s", (request.form['email'],))
|
c.execute("SELECT * FROM users WHERE email = %s", (request.form['email'],))
|
||||||
data = c.fetchone()
|
data = c.fetchone()
|
||||||
c.close()
|
c.close()
|
||||||
|
if data == None:
|
||||||
|
session['error'] = "Incorrect login information."
|
||||||
|
return redirect(url_for("show_login_page"), 303)
|
||||||
|
|
||||||
if bcrypt.checkpw(pw_hashed, data['password']):
|
if bcrypt.checkpw(pw_hashed, data['password']):
|
||||||
session['user_id'] = data['id']
|
session['user_id'] = data['id']
|
||||||
return redirect(url_for("home"))
|
return redirect(url_for("home"))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return "invalid login"
|
session['error'] = "Incorrect login information."
|
||||||
|
return redirect(url_for("show_login_page"), 303)
|
||||||
|
|
||||||
@app.route("/img/bot_generic.png")
|
@app.route("/img/bot_generic.png")
|
||||||
def img_bot_generic():
|
def img_bot_generic():
|
||||||
|
|
Loading…
Reference in a new issue