diff --git a/frontend/app.py b/frontend/app.py index af342a6..1bd7b5e 100644 --- a/frontend/app.py +++ b/frontend/app.py @@ -1,5 +1,10 @@ from flask import Flask, render_template, request, redirect, url_for, flash +import hashlib + +def hash_password(password): + return hashlib.sha512(password.strip().encode('utf-8')).hexdigest() + app = Flask(__name__) app.secret_key = 'your_secret_key' @@ -7,25 +12,59 @@ app.secret_key = 'your_secret_key' def home(): return redirect(url_for('login')) +@app.route('/register', methods=['GET', 'POST']) +def register(): + import sqlLite.get as getter + import sqlLite.set as setter + import useful.hash as hasher + if request.method == 'POST': + username = request.form['username'].strip() + email = request.form.get('email').strip() + password = request.form.get('password').strip() + pwd_confirm = request.form.get('confirm_password').strip() + + if not email or not password or not pwd_confirm or not username: + flash('Please fill out all fields', 'error') + return redirect(url_for('register')) + + if password != pwd_confirm: + flash('Passwords do not match', 'error') + return redirect(url_for('register')) + # Hash the password + hashed_password = hasher.sha512(password.encode('utf-8')).hexdigest() + try: + # Call your setter function to add user to DB + setter.set_login(username, email, hashed_password) + flash('Registration successful! Please log in.', 'success') + return redirect(url_for('login')) + except Exception as e: + flash(f'Error: {str(e)}', 'error') + return redirect(url_for('register')) + + return render_template('register.html') @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': enter_email = request.form.get('email') enter_password = request.form.get('password') + import hashlib import sqlLite.get as getter - import useful.hash as hasher - pwd = getter.get_password_by_email(enter_email) - password = hasher.get_password_hash(enter_password) - if password == pwd: - return redirect(url_for('dashboard')) - elif password == None: + + stored_hash = getter.get_password_by_email(enter_email) # use email here + + if stored_hash is None: flash("User not found!") return redirect(url_for("login")) - elif pwd == None: - flash("Password not found!") - return redirect(url_for("login")) + + hash_entered = hashlib.sha512(enter_password.encode('utf-8')).hexdigest() + + if hash_entered == stored_hash: + return redirect(url_for('dashboard')) else: flash('Invalid email or password', 'error') + print("Stored hash:", stored_hash) + print("Entered hash:", hash_entered) + return redirect(url_for('login')) return render_template('login.html') diff --git a/templates/login.html b/frontend/templates/login.html similarity index 100% rename from templates/login.html rename to frontend/templates/login.html diff --git a/frontend/templates/register.html b/frontend/templates/register.html new file mode 100644 index 0000000..e735073 --- /dev/null +++ b/frontend/templates/register.html @@ -0,0 +1,151 @@ + + +
+ + +