Add Sign Up
This commit is contained in:
parent
75c2873ff3
commit
3067d0c118
39
src/app.py
39
src/app.py
@ -26,21 +26,33 @@ 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']
|
||||||
my_user = User.query.filter_by(
|
if request.form['action'] == 'signup':
|
||||||
email=data['email'], password=data['password']).first()
|
# New user
|
||||||
if my_user:
|
# Email check
|
||||||
# Create user session
|
my_user = User.query.filter_by(
|
||||||
session['user_id'] = my_user.id
|
email=data['email']).first()
|
||||||
session['user_email'] = my_user.email
|
if not my_user:
|
||||||
return redirect(url_for('dashboard'))
|
# Create
|
||||||
else:
|
my_user = User(data['email'], data['password'])
|
||||||
data['error'] = True
|
db.session.add(my_user)
|
||||||
else:
|
db.session.commit()
|
||||||
data['error'] = True
|
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)
|
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()
|
||||||
|
@ -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 %}
|
||||||
|
Loading…
Reference in New Issue
Block a user