TO_DO: FIX ISSUE WITH REGISTERING

This commit is contained in:
florianuhlig
2025-10-03 00:52:43 +02:00
parent 740fe629b3
commit 823ea8cfc4
5 changed files with 210 additions and 20 deletions

View File

@@ -1,5 +1,10 @@
from flask import Flask, render_template, request, redirect, url_for, flash 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 = Flask(__name__)
app.secret_key = 'your_secret_key' app.secret_key = 'your_secret_key'
@@ -7,25 +12,59 @@ app.secret_key = 'your_secret_key'
def home(): def home():
return redirect(url_for('login')) 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']) @app.route('/login', methods=['GET', 'POST'])
def login(): def login():
if request.method == 'POST': if request.method == 'POST':
enter_email = request.form.get('email') enter_email = request.form.get('email')
enter_password = request.form.get('password') enter_password = request.form.get('password')
import hashlib
import sqlLite.get as getter import sqlLite.get as getter
import useful.hash as hasher
pwd = getter.get_password_by_email(enter_email) stored_hash = getter.get_password_by_email(enter_email) # use email here
password = hasher.get_password_hash(enter_password)
if password == pwd: if stored_hash is None:
return redirect(url_for('dashboard'))
elif password == None:
flash("User not found!") flash("User not found!")
return redirect(url_for("login")) return redirect(url_for("login"))
elif pwd == None:
flash("Password not found!") hash_entered = hashlib.sha512(enter_password.encode('utf-8')).hexdigest()
return redirect(url_for("login"))
if hash_entered == stored_hash:
return redirect(url_for('dashboard'))
else: else:
flash('Invalid email or password', 'error') 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') return render_template('login.html')

View File

@@ -0,0 +1,151 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Register</title>
<style>
/* Use the same styling as login.html for consistency */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;600&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body {
height: 100vh;
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.login-container {
background: white;
max-width: 400px;
width: 100%;
padding: 40px 30px 50px;
border-radius: 20px;
box-shadow: 0 20px 40px rgba(0,0,0,0.2);
text-align: center;
transition: transform 0.3s ease;
}
.login-container:hover {
transform: translateY(-8px);
box-shadow: 0 30px 60px rgba(0,0,0,0.3);
}
h2 {
font-weight: 600;
color: #333;
margin-bottom: 30px;
font-size: 28px;
letter-spacing: 1.2px;
}
.input-group {
margin-bottom: 25px;
text-align: left;
}
label {
display: block;
margin-bottom: 8px;
color: #555;
font-weight: 600;
font-size: 14px;
}
input[type="username"],
input[type="email"],
input[type="password"] {
width: 100%;
padding: 14px 18px;
font-size: 16px;
border-radius: 12px;
border: 2px solid #ddd;
transition: 0.3s border-color ease;
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
}
input[type="email"]:focus,
input[type="password"]:focus {
border-color: #2575fc;
outline: none;
box-shadow: 0 0 12px rgba(37, 117, 252, 0.5);
}
button {
width: 100%;
padding: 16px 0;
margin-top: 10px;
background-color: #2575fc;
border: none;
border-radius: 14px;
color: white;
font-size: 18px;
font-weight: 700;
letter-spacing: 1px;
cursor: pointer;
box-shadow: 0 10px 20px rgba(37, 117, 252, 0.4);
transition: background-color 0.3s ease, box-shadow 0.3s ease;
}
button:hover {
background-color: #1859d6;
box-shadow: 0 15px 25px rgba(24, 89, 214, 0.6);
}
.error-message {
margin-top: 20px;
color: #ff4d4f;
font-weight: 600;
font-size: 15px;
text-align: center;
background: #ffe6e6;
padding: 10px 15px;
border-radius: 10px;
box-shadow: 0 2px 6px rgba(255,77,79,0.3);
display: none;
}
</style>
</head>
<body>
<div class="login-container">
<h2>Create an Account</h2>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<div class="error-message" style="display:block;">
{% for category, message in messages %}
{{ message }}
{% endfor %}
</div>
{% endif %}
{% endwith %}
<form method="POST" action="{{ url_for('register') }}">
<div class="input-group">
<label for="username">Username</label>
<input type="username" id="username" name="username" placeholder="username" required />
</div>
<div class="input-group">
<label for="email">Email Address</label>
<input type="email" id="email" name="email" placeholder="you@example.com" required />
</div>
<div class="input-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Create a password" required />
</div>
<div class="input-group">
<label for="confirm_password">Confirm Password</label>
<input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm your password" required />
</div>
<button type="submit">Register</button>
<p style="margin-top: 20px; font-size: 14px;">Already have an account? <a href="{{ url_for('login') }}">Log in here</a>.</p>
</form>
</div>
</body>
</html>

21
main.py
View File

@@ -1,20 +1,19 @@
db_type = "sqlite"
import sqlLite import sqlLite
from frontend.app import app from frontend.app import app
sqlLite.set_db_name("databases/test.db") sqlLite.set_db_name("databases/test.db")
if db_type == "sqlite": import sqlLite.create as create
import sqlLite.create as create import sqlLite.set as setter
import sqlLite.set as setter import sqlLite.get as getter
import sqlLite.get as getter #import testing as testing
#import testing as testing
#testing.sqllite_reset() #testing.sqllite_reset()
create.create_table_t_user() create.create_table_t_user()
setter.set_login("test", "test@test.test", "password") setter.set_login("test", "test@test.test", "password")
setter.set_login("admin","admin@test.test", "admin") #setter.set_login("admin", "admin@test.test", "admin")
getter.get_password_by_email("admin@fuhlig.de") #getter.get_password_by_email("admin@fuhlig.de")
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080) app.run(debug=True, host='0.0.0.0', port=8080)

View File

@@ -1,4 +1,5 @@
from hashlib import sha512 from hashlib import sha512
def get_password_hash(password): def get_password_hash(password):
password = password.strip()
return sha512(password.encode('utf-8')).hexdigest() return sha512(password.encode('utf-8')).hexdigest()