Commit 928b04dd authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

login added

parent 0e9e4833
...@@ -35,16 +35,21 @@ def chk_user(name, password): ...@@ -35,16 +35,21 @@ def chk_user(name, password):
conn = psycopg2.connect('dbname=gateway') conn = psycopg2.connect('dbname=gateway')
cur = conn.cursor() cur = conn.cursor()
query = """ query = """
INSERT INTO SELECT * FROM
users users
VALUES WHERE
(%s, %s) name = %s
""" """
cur.execute(query, (name, bcrypt.hashpw(password, bcrypt.gensalt()))) cur.execute(query, (name,))
conn.commit() user = cur.fetchall()[0]
print('User added')
if user[1].encode('utf-8') == bcrypt.hashpw(password, user[1].encode('utf-8')):
session['name'] = user[0]
print('User logged in')
else:
suc = (False, 'Password or username do not match')
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg2.DatabaseError) as error:
print('Error adding a user: ', error) print('Error querying a user: ', error)
suc = (False, error) suc = (False, error)
finally: finally:
if (conn): if (conn):
...@@ -54,7 +59,6 @@ def chk_user(name, password): ...@@ -54,7 +59,6 @@ def chk_user(name, password):
return suc return suc
@app.route('/') @app.route('/')
def index(): def index():
return render_template('index.html') return render_template('index.html')
...@@ -65,7 +69,7 @@ def signup(): ...@@ -65,7 +69,7 @@ def signup():
return render_template('signup.html') return render_template('signup.html')
else: else:
username = request.form['username'] username = request.form['username']
password = request.form['password'] password = request.form['password'].encode('utf-8')
if (username == '' or password == ''): if (username == '' or password == ''):
feedback = 'Username or password fields cannot be empty' feedback = 'Username or password fields cannot be empty'
...@@ -74,18 +78,18 @@ def signup(): ...@@ -74,18 +78,18 @@ def signup():
res, msg = new_user(username, password) res, msg = new_user(username, password)
if (not res): if (not res):
return render_template('signup.html', feedback=msg) return render_template('signup.html', feedback=msg)
else:
session['name'] = username session['name'] = username
return redirect(url_for('index')) return redirect(url_for('index'))
@app.route('/login', methods=['GET', 'POST']) @app.route('/login', methods=['GET', 'POST'])
def signup(): def login():
if request.method == 'GET': if request.method == 'GET':
return render_template('login.html') return render_template('login.html')
else: else:
username = request.form['username'] username = request.form['username']
password = request.form['password'] password = request.form['password'].encode('utf-8')
if (username == '' or password == ''): if (username == '' or password == ''):
feedback = 'Username or password fields cannot be empty' feedback = 'Username or password fields cannot be empty'
...@@ -93,13 +97,19 @@ def signup(): ...@@ -93,13 +97,19 @@ def signup():
else: else:
res, msg = chk_user(username, password) res, msg = chk_user(username, password)
if (not res): if (not res):
return render_template('signup.html', feedback=msg) return render_template('login.html', feedback=msg)
else:
session['name'] = username session['name'] = username
return redirect(url_for('index')) return redirect(url_for('index'))
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('index'))
@app.route('/apps') @app.route('/apps')
def apps(): def apps():
return '<h1>Manage your apps, ' + app.conf['username'] + '</h1>' return '<h1>Manage your apps, ' + app.conf['username'] + '</h1>'
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
</div> </div>
<div id="navbar" class="collapse navbar-collapse"> <div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li><a href="#">Home</a></li> <li><a href="/index">Home</a></li>
{% if session['name'] %} {% if session['name'] %}
<li><a href="/logout">Logout</a></li> <li><a href="/logout">Logout</a></li>
{% else %} {% else %}
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<div class="row"> <div class="row">
<div class="col-md-6 col-md-offset3"> <div class="col-md-6 col-md-offset3">
<form action="signup" method="post"> <form action="login" method="post">
<div class="form-group"> <div class="form-group">
<label>Username:</label><br> <label>Username:</label><br>
<input type="text" id="username" name="username"><br> <input type="text" id="username" name="username"><br>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment