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,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()

View File

@ -1,21 +1,31 @@
{% extends 'layouts/master.html' %}
{% block contain %}
{% if data['error'] %}
{% if data['error'] and data['error'] == 'login' %}
<div class="row">
<div class="alert alert-danger" role="alert">
<strong>Error</strong> Email or password incorrect
</div>
</div>
{% 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">
<form method="post" action="{{ url_for('login') }}">
<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 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>
<button type="submit" class="btn btn-default center-block">Login</button>
</form>
</div>
{% endblock %}