diff --git a/app.py b/app.py index 85df982..201ae96 100644 --- a/app.py +++ b/app.py @@ -1,11 +1,17 @@ #!/usr/bin/env python3 -from flask import Flask, render_template, request, session +from flask import Flask, render_template, request, session, redirect, url_for import logging +from flask_debugtoolbar import DebugToolbarExtension from mysql import connector +from passlib.hash import sha256_crypt app = Flask(__name__) +# !--- For debugging switch to true ---! +app.debug = False + app.config["SECRET_KEY"] = "OCML3BRawWEUeaxcuKHLpw" +toolbar = DebugToolbarExtension(app) def definedlog(fileHandler): @@ -29,6 +35,24 @@ def connect_db(host, user, password, database): conn = connect_db('localhost', 'root', 'LoginPass@@11223344', 'tiger') +@app.route('/sign_up', methods=['GET', 'POST']) +def sign_up(): + if request.method == 'POST': + userDetails = request.form + username = userDetails['username'] + password = sha256_crypt.encrypt(userDetails["password"]) + mycursor = conn.cursor() + sql = "INSERT INTO users (username, password) VALUES (%s, %s)" + val = (username, password) + mycursor.execute(sql, val) + conn.commit() + # if authenticate_user(username, password): + session["USERNAME"] = username + session["PASSWORD"] = password + return redirect(url_for('send_message')) + return render_template('/sign_up.html') + + @app.route('/') def home(): return render_template('/home.html') @@ -43,30 +67,34 @@ def contact_us(): def send_message(): if request.method == 'POST': userDetails = request.form - username = userDetails['username'] + username = session["USERNAME"] msg = userDetails['content'] mycursor = conn.cursor() sql = "INSERT INTO messages (username, content) VALUES (%s, %s)" val = (username, msg) mycursor.execute(sql, val) conn.commit() + return redirect(url_for('messages_view')) return render_template('/send_message.html') -def authenticate_user(username, password): - if check_username(username): - if check_password(username, password): - return True - else: - # TODO: flash a message about incorrect password - return False - # TODO: flash a message about incorrect username +def check_username(username, pas): + maulers = conn.cursor() + Fender = "SELECT * FROM users" + maulers.execute(Fender) + result = maulers.fetchall() + for user in result: + print(user) + if user[0] == username: + if sha256_crypt.verify(pas, user[1]): + return True return False -def check_password(username, password): - # TODO: check password against the db - pass +def authenticate_user(username, password): + if check_username(username, password): + return True + return False @app.route('/messages_view', methods=['GET', 'POST']) @@ -87,32 +115,24 @@ def messages_view(): def log_in(): if request.method == "POST": req = request.form - email = req.get("email") + username = req.get("username") password = req.get("password") - # if authenticate_user(username, password): - session["EMAIL"] = email - session["PASSWORD"] = password - return render_template('home.html', email=session["EMAIL"]) - return render_template('/sign_up.html') + if authenticate_user(username, password): + session["USERNAME"] = username + session["PASSWORD"] = password + return redirect(url_for('send_message')) + else: + return redirect(url_for('log_in')) + return render_template('send_message.html', + username=session["USERNAME"]) + return render_template('/sign_in.html') @app.route('/log_out') def log_out(): + session.clear() return render_template('home.html') if __name__ == '__main__': app.run(host='0.0.0.0') - - -def check_username(username): - maulers = conn.cursor() - Fender = "SELECT * FROM users WHERE username =" + username - maulers.execute(Fender) - result = maulers.fetchall() - if not result: - # the user doesnt exist - return False - else: - # the user exists - return True diff --git a/requirements.txt b/requirements.txt index a754bdf..d6b6de9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ Flask -mysql-connector-python \ No newline at end of file +mysql-connector-python +passlib +flask-debugtoolbar \ No newline at end of file diff --git a/templates/home.html b/templates/home.html index 6577769..b0d2370 100644 --- a/templates/home.html +++ b/templates/home.html @@ -7,7 +7,7 @@

Welcome to The Academic College of Tel Aviv-Yaffo chat-App!!!

This app will allow you to send a message and view it.

You can view our app's messages whenever you want!

If you would like to use our app, you need to follow these steps:

-

1. Register / Log in

+

1. Register / Log in

2. Send a message

If you would like to read about us and our collaboration with the RedHat team, you can click on this diff --git a/templates/layout.html b/templates/layout.html index 29c4719..af47e47 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -1,38 +1,52 @@ + + + {% block head %} + {% block title %}{% endblock %} - ChatApp + {% endblock %} + - - - {% block head %} - {% block title %}{% endblock %} - ChatApp - {% endblock %} - - - - +
- - - + + +
- - -
- {% block content %}{% endblock %} +
+ {% block content %}{% endblock %}
- - - + diff --git a/templates/send_message.html b/templates/send_message.html index 08ba052..e9dd53b 100644 --- a/templates/send_message.html +++ b/templates/send_message.html @@ -1,46 +1,59 @@ - - + - - - - - - - - - - - - - Tiger - - - - - - - {% extends "layout.html" %} - {% block content %} - -

Send Message

-

- Enter your email
- Enter your message
-
-

-
-

- -

- - - - {% endblock %} - - - + + + + + + + + + + + + Tiger + + + + + + {% extends "layout.html" %} {% block content %} + +

Send Message

+ {% if not session.get("USERNAME") %} +

Please register or log in

+ {% else %} +
+
+ Enter your user name + +
+ Enter your message
+
+

+ +
+

+ + +
+ {% endif %} +
+ + {% endblock %} + diff --git a/templates/sign_in.html b/templates/sign_in.html new file mode 100644 index 0000000..71b4131 --- /dev/null +++ b/templates/sign_in.html @@ -0,0 +1,90 @@ + + + + + + + Sign-in + + + {% extends "layout.html" %} {% block content %} +
+ +


+
+ {% endblock %} + + diff --git a/templates/sign_up.html b/templates/sign_up.html index 9431d07..a35c315 100644 --- a/templates/sign_up.html +++ b/templates/sign_up.html @@ -1,98 +1,87 @@ - - - - + Sign-in + + + {% extends "layout.html" %} {% block content %} +
+ +
+ {% endblock %} + +