From 3067d0c1189bf2c6196ef62f3bfcffa4cc4c9740 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sun, 18 Sep 2016 13:46:32 +0200 Subject: [PATCH] Add Sign Up --- src/app.py | 39 ++++++++++++++++++++++------------ src/templates/items/login.html | 18 ++++++++++++---- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/app.py b/src/app.py index 9305550..388f96d 100644 --- a/src/app.py +++ b/src/app.py @@ -26,21 +26,33 @@ def index(data=None): @app.route('/login', methods=['POST']) def login(): data = dict() - if request.form['email'] and request.form['password']: - # Checks if the user exists + if request.form['action'] and request.form['email'] and request.form['password']: data['email'] = request.form['email'] data['password'] = request.form['password'] - my_user = User.query.filter_by( - email=data['email'], password=data['password']).first() - if my_user: - # Create user session - session['user_id'] = my_user.id - session['user_email'] = my_user.email - return redirect(url_for('dashboard')) - else: - data['error'] = True - else: - data['error'] = True + if request.form['action'] == 'signup': + # New user + # Email check + my_user = User.query.filter_by( + email=data['email']).first() + if not my_user: + # Create + my_user = User(data['email'], data['password']) + db.session.add(my_user) + db.session.commit() + else: + data['error'] = 'register' + + # Checks if the user exists + if 'error' not in data: + my_user = User.query.filter_by( + email=data['email'], password=data['password']).first() + if my_user: + # Create user session + session['user_id'] = my_user.id + session['user_email'] = my_user.email + return redirect(url_for('dashboard')) + else: + data['error'] = 'login' return index(data) @@ -162,4 +174,5 @@ def delete_note(): # App if __name__ == "__main__": app.secret_key = 'secret' + app.debug = True app.run() diff --git a/src/templates/items/login.html b/src/templates/items/login.html index 305bf99..121e86b 100644 --- a/src/templates/items/login.html +++ b/src/templates/items/login.html @@ -1,21 +1,31 @@ {% extends 'layouts/master.html' %} {% block contain %} -{% if data['error'] %} +{% if data['error'] and data['error'] == 'login' %}
{% endif %} +{% if data['error'] and data['error'] == 'register' %} +
+ +
+{% endif %}
- +
- + +
+
+ +
-
{% endblock %}