Add Sign Up

This commit is contained in:
Andros Fenollosa 2016-09-18 13:46:32 +02:00
parent 75c2873ff3
commit 3067d0c118
2 changed files with 40 additions and 17 deletions

View File

@ -26,10 +26,24 @@ def index(data=None):
@app.route('/login', methods=['POST']) @app.route('/login', methods=['POST'])
def login(): def login():
data = dict() data = dict()
if request.form['email'] and request.form['password']: if request.form['action'] and request.form['email'] and request.form['password']:
# Checks if the user exists
data['email'] = request.form['email'] data['email'] = request.form['email']
data['password'] = request.form['password'] data['password'] = request.form['password']
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( my_user = User.query.filter_by(
email=data['email'], password=data['password']).first() email=data['email'], password=data['password']).first()
if my_user: if my_user:
@ -38,9 +52,7 @@ def login():
session['user_email'] = my_user.email session['user_email'] = my_user.email
return redirect(url_for('dashboard')) return redirect(url_for('dashboard'))
else: else:
data['error'] = True data['error'] = 'login'
else:
data['error'] = True
return index(data) return index(data)
@ -162,4 +174,5 @@ def delete_note():
# App # App
if __name__ == "__main__": if __name__ == "__main__":
app.secret_key = 'secret' app.secret_key = 'secret'
app.debug = True
app.run() app.run()

View File

@ -1,21 +1,31 @@
{% extends 'layouts/master.html' %} {% extends 'layouts/master.html' %}
{% block contain %} {% block contain %}
{% if data['error'] %} {% if data['error'] and data['error'] == 'login' %}
<div class="row"> <div class="row">
<div class="alert alert-danger" role="alert"> <div class="alert alert-danger" role="alert">
<strong>Error</strong> Email or password incorrect <strong>Error</strong> Email or password incorrect
</div> </div>
</div> </div>
{% endif %} {% endif %}
{% if data['error'] and data['error'] == 'register' %}
<div class="row">
<div class="alert alert-danger" role="alert">
<strong>Error</strong> Email exists
</div>
</div>
{% endif %}
<div class="row"> <div class="row">
<form method="post" action="{{ url_for('login') }}"> <form method="post" action="{{ url_for('login') }}">
<div class="form-group"> <div class="form-group">
<input type="email" class="form-control" id="email" placeholder="Email" name="email" value="{{ data['email'] }}"> <input type="email" class="form-control" required id="email" placeholder="Email" name="email" value="{{ data['email'] }}">
</div> </div>
<div class="form-group"> <div class="form-group">
<input type="password" class="form-control" id="password" placeholder="Password" name="password" value="{{ data['password'] }}"> <input type="password" class="form-control" required id="password" placeholder="Password" name="password" value="{{ data['password'] }}">
</div>
<div class="text-center">
<button type="submit" class="btn btn-default" name="action" value="login">LogIn</button>
<button type="submit" class="btn btn-default" name="action" value="signup">SignUp</button>
</div> </div>
<button type="submit" class="btn btn-default center-block">Login</button>
</form> </form>
</div> </div>
{% endblock %} {% endblock %}