Improved login funtion by creating temporary HTML pages and fixing bugs in login

This commit is contained in:
white
2025-01-17 16:58:23 +00:00
parent a7b62ff972
commit e89e5f0fef
13 changed files with 84 additions and 39 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -4,13 +4,13 @@ from werkzeug.security import generate_password_hash, check_password_hash
from functools import wraps
from core.forms import SignupForm, LoginForm
from core.database import Database
from database.database import Database
app = Flask(__name__, template_folder="../ui/templates/")
app.config["SECRET_KEY"] = ""
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
app.teardown_appcontext(Database.close_connection)
Session(app)
@app.before_request
@@ -58,7 +58,7 @@ def signup():
# Store in database and hash to avoid exposing sensitive information
db = Database()
cursor = db.create_connection("../database/app.db")
cursor = db.create_connection()
# Check if user already exists to avoid duplicates
dup_email = cursor.execute("""SELECT * FROM users
@@ -73,9 +73,9 @@ def signup():
elif password != password2:
form.password.errors.append("Passwords must match.")
else:
db.execute("""INSERT INTO users (username, password, email, num_followers, isPartenered, bio)
cursor.execute("""INSERT INTO users (username, password, email, num_followers, isPartenered, bio)
VALUES (?, ?, ?, ?, ?, ?);""", (username, generate_password_hash(password), email, 0, 0, "This user does not have a Bio."))
db.commit()
db.commit_data()
return redirect(url_for("login"))
@@ -94,7 +94,7 @@ def login():
# Compare with database
db = Database()
cursor = db.create_connection("../database/app.db")
cursor = db.create_connection()
# Check if user exists so only users who have signed up can login
user_exists = cursor.execute("""SELECT * FROM users

View File

@@ -1,15 +0,0 @@
import sqlite3
class Database:
def __init__(self, db:str) -> None:
self._db = db
self._conn = None
def create_connection(self) -> sqlite3.Cursor:
conn = sqlite3.connect(self._db)
self._conn = conn
cursor = conn.cursor()
return cursor
def close_connection(self) -> None:
self._conn.close()

Binary file not shown.

Binary file not shown.

22
database/database.py Normal file
View File

@@ -0,0 +1,22 @@
import sqlite3
import os
class Database:
def __init__(self) -> None:
self._db = os.path.join(os.path.abspath(os.path.dirname(__file__)), "app.db")
def create_connection(self) -> sqlite3.Cursor:
conn = sqlite3.connect(self._db)
conn.row_factory = sqlite3.Row
self._conn = conn
cursor = conn.cursor()
return cursor
def commit_data(self):
try:
self._conn.commit()
except Exception as e:
print(e)
def close_connection(self) -> None:
self._conn.close()

View File

@@ -5,10 +5,13 @@ CREATE TABLE users
password VARCHAR(256) NOT NULL,
email VARCHAR(64) NOT NULL,
num_followers INTEGER NOT NULL,
isPartenered BOOLEAN NOT NULL DEFAULT 0
bio TEXT,
isPartenered BOOLEAN NOT NULL DEFAULT 0,
bio TEXT
);
SELECT * FROM users;
DROP TABLE IF EXISTS streams;
CREATE TABLE streams
(

Binary file not shown.

Binary file not shown.

View File

@@ -1,20 +1,5 @@
{% extends "base.html" %}
{% block main_content %}
<h1>Live Stream</h1>
<video id="localVideo" autoplay playsinline controls></video>
<script>
// constant containing the video
const videoElement = document.getElementById('localVideo');
// Request access to the user's camera and microphone
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
.then((stream) => {
// Display the stream locally
videoElement.srcObject = stream;
})
.catch((error) => {
console.error('Error accessing media devices.', error);
});
</script>
<h1>Welcome</h1>
{% endblock %}

19
ui/templates/login.html Normal file
View File

@@ -0,0 +1,19 @@
{% extends "base.html" %}
{% block main_content %}
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
{{ form.username.label }}
{{ form.username() }}
{% for error in form.username.errors %}
{{ error }}
{% endfor %}
<br />
{{ form.password.label }}
{{ form.password() }}
<br />
{{ form.submit() }}
</form>
{% endblock %}

31
ui/templates/signup.html Normal file
View File

@@ -0,0 +1,31 @@
{% extends "base.html" %}
{% block main_content %}
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
{{ form.email.label }}
{{ form.email() }}
{% for error in form.email.errors %}
{{ error }}
{% endfor %}
<br />
{{ form.username.label }}
{{ form.username() }}
{% for error in form.username.errors %}
{{ error }}
{% endfor %}
<br />
{{ form.password.label }}
{{ form.password() }}
{% for error in form.password.errors %}
{{ error }}
{% endfor %}
<br />
{{ form.password2.label }}
{{ form.password2() }}
<br />
{{ form.submit() }}
</form>
{% endblock %}