Created login functinality
This commit is contained in:
parent
b26bd1a228
commit
e0b04d13f6
@ -1,4 +1,5 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
from typing import Mapping, Any
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
class DatabaseController(ABC):
|
class DatabaseController(ABC):
|
||||||
@ -14,6 +15,12 @@ class DatabaseController(ABC):
|
|||||||
self._conn.close()
|
self._conn.close()
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
def new_instance(self, of: type, with_fields: Mapping[str, Any]):
|
||||||
|
obj = of.__new__(of)
|
||||||
|
for attr, value in with_fields.items():
|
||||||
|
setattr(obj, attr, value)
|
||||||
|
return obj
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def create(self):
|
def create(self):
|
||||||
pass
|
pass
|
||||||
|
@ -1,30 +1,48 @@
|
|||||||
from .database import DatabaseController
|
from .database import DatabaseController
|
||||||
from models.users.user import User
|
from models.users.user import User
|
||||||
|
from models.users.customer import Customer
|
||||||
|
|
||||||
class UserController(DatabaseController):
|
class UserController(DatabaseController):
|
||||||
|
FIELDS = ['id', 'username', 'password', 'firstName', 'lastName', 'email', 'phone', 'role']
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def create(self, user: User):
|
def create(self, user: User):
|
||||||
params = [
|
params = [
|
||||||
|
user.username,
|
||||||
|
user.password,
|
||||||
user.firstName,
|
user.firstName,
|
||||||
user.lastName,
|
user.lastName,
|
||||||
user.email,
|
user.email,
|
||||||
user.phone,
|
user.phone,
|
||||||
user.password,
|
|
||||||
user.role
|
user.role
|
||||||
]
|
]
|
||||||
|
|
||||||
self._conn.execute(
|
self._conn.execute(
|
||||||
"INSERT INTO Users (first_name, last_name, email, phone, password, role) VALUES (?, ?, ?, ?, ?, ?)",
|
"INSERT INTO Users (username, password, first_name, last_name, email, phone, role) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||||
params
|
params
|
||||||
)
|
)
|
||||||
self._conn.commit()
|
self._conn.commit()
|
||||||
user.login()
|
|
||||||
|
|
||||||
def read(self):
|
def read(self, username: str) -> User | None:
|
||||||
print("Doing work")
|
params = [
|
||||||
|
username
|
||||||
|
]
|
||||||
|
|
||||||
|
cursor = self._conn.execute(
|
||||||
|
"SELECT * FROM Users WHERE Username = ?",
|
||||||
|
params
|
||||||
|
)
|
||||||
|
row = cursor.fetchone()
|
||||||
|
|
||||||
|
if row != None:
|
||||||
|
params = dict(zip(self.FIELDS, row))
|
||||||
|
obj = self.new_instance(Customer, params)
|
||||||
|
print(obj.__dict__)
|
||||||
|
return obj
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
print("Doing work")
|
print("Doing work")
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
from . import blueprint
|
from . import blueprint
|
||||||
from flask import render_template, redirect, request
|
from flask import render_template, redirect, request, session
|
||||||
from controllers.database.user import UserController
|
from controllers.database.user import UserController
|
||||||
from models.users.customer import Customer
|
from models.users.customer import Customer
|
||||||
from hashlib import sha512
|
from hashlib import sha512
|
||||||
@ -8,7 +8,7 @@ from hashlib import sha512
|
|||||||
# Function responsible for displaying the main landing page of the site
|
# Function responsible for displaying the main landing page of the site
|
||||||
@blueprint.route('/')
|
@blueprint.route('/')
|
||||||
def welcome_page():
|
def welcome_page():
|
||||||
return render_template('index.html', content="content.html")
|
return render_template('index.html', content="content.html", user = session.get('user'))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -16,12 +16,23 @@ def welcome_page():
|
|||||||
# Function responsible for delivering the Login page for the site
|
# Function responsible for delivering the Login page for the site
|
||||||
@blueprint.route('/login')
|
@blueprint.route('/login')
|
||||||
def display_login():
|
def display_login():
|
||||||
return render_template('index.html', content="login.html")
|
return render_template('index.html', content="login.html", user = session.get('user'))
|
||||||
|
|
||||||
# Function responsible for handling logins to the site
|
# Function responsible for handling logins to the site
|
||||||
@blueprint.post('/login')
|
@blueprint.post('/login')
|
||||||
def login():
|
def login():
|
||||||
print("Tryin to login as " + request.form['username'])
|
database = UserController()
|
||||||
|
user = database.read(request.form['username'])
|
||||||
|
|
||||||
|
# No user found
|
||||||
|
if user == None:
|
||||||
|
return redirect("/login")
|
||||||
|
|
||||||
|
# Incorrect Password
|
||||||
|
if sha512(request.form['password'].encode()).hexdigest() != user.password:
|
||||||
|
return redirect("/login")
|
||||||
|
|
||||||
|
session['user'] = user.username
|
||||||
return redirect("/")
|
return redirect("/")
|
||||||
|
|
||||||
|
|
||||||
@ -29,7 +40,7 @@ def login():
|
|||||||
# Function responsible for delivering the Signup page for the site
|
# Function responsible for delivering the Signup page for the site
|
||||||
@blueprint.route('/signup')
|
@blueprint.route('/signup')
|
||||||
def display_signup():
|
def display_signup():
|
||||||
return render_template('index.html', content="signup.html")
|
return render_template('index.html', content="signup.html", user = session.get('user'))
|
||||||
|
|
||||||
# Function responsible for handling signups to the site
|
# Function responsible for handling signups to the site
|
||||||
@blueprint.post('/signup')
|
@blueprint.post('/signup')
|
||||||
@ -38,12 +49,20 @@ def signup():
|
|||||||
database.create(Customer(
|
database.create(Customer(
|
||||||
0,
|
0,
|
||||||
request.form['username'],
|
request.form['username'],
|
||||||
request.form['email'],
|
sha512(request.form['password'].encode()).hexdigest(), # Hashed as soon as it is recieved on the backend
|
||||||
request.form['firstname'],
|
request.form['firstname'],
|
||||||
request.form['lastname'],
|
request.form['lastname'],
|
||||||
|
request.form['email'],
|
||||||
"123",
|
"123",
|
||||||
sha512(request.form['password'].encode()).hexdigest(), # Hashed as soon as it is recieved on the backend
|
|
||||||
"Customer"
|
"Customer"
|
||||||
))
|
))
|
||||||
|
|
||||||
return redirect("/")
|
# Code 307 Preserves the original request (POST)
|
||||||
|
return redirect("/login", code=307)
|
||||||
|
|
||||||
|
|
||||||
|
# Function responsible for handling logouts from the site
|
||||||
|
@blueprint.route('/logout')
|
||||||
|
def logout():
|
||||||
|
session.pop('user')
|
||||||
|
return redirect("/")
|
@ -2,24 +2,12 @@ from .user import User
|
|||||||
|
|
||||||
class Admin(User):
|
class Admin(User):
|
||||||
'''
|
'''
|
||||||
Class constructor to instatiate an Admin object
|
Class constructor to instatiate an admin object
|
||||||
|
|
||||||
|
No additional properties are assigned to the admin
|
||||||
'''
|
'''
|
||||||
def __init__(self):
|
def __init__(self, id: int, username: str, password: str, firstname: str,
|
||||||
super().__init__()
|
lastname: str, email: str, phone: str, role: str):
|
||||||
self.store = ""
|
super().__init__(
|
||||||
|
id, username, password, firstname, lastname, email, phone, role
|
||||||
def login(self):
|
)
|
||||||
print("Logging in as Admin")
|
|
||||||
|
|
||||||
def signup(self):
|
|
||||||
print("Signing up as Admin")
|
|
||||||
|
|
||||||
def createProduct():
|
|
||||||
pass
|
|
||||||
|
|
||||||
def deleteProduct():
|
|
||||||
pass
|
|
||||||
|
|
||||||
def updateProduct():
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
@ -6,15 +6,9 @@ class Customer(User):
|
|||||||
|
|
||||||
No additional properties are assigned to the customer
|
No additional properties are assigned to the customer
|
||||||
'''
|
'''
|
||||||
def __init__(self, id: int, username: str, email: str, firstname: str,
|
def __init__(self, id: int, username: str, password: str, firstname: str,
|
||||||
lastname: str, phone: str, password: str, role: str):
|
lastname: str, email: str, phone: str, role: str):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
id, username, email, firstname, lastname, phone, password, role
|
id, username, password, firstname, lastname, email, phone, role
|
||||||
)
|
)
|
||||||
|
|
||||||
def login(self):
|
|
||||||
print("Logging in as Customer")
|
|
||||||
|
|
||||||
def signup(self):
|
|
||||||
print("Signing up as Customer")
|
|
||||||
|
|
||||||
|
@ -2,24 +2,13 @@ from .user import User
|
|||||||
|
|
||||||
class Seller(User):
|
class Seller(User):
|
||||||
'''
|
'''
|
||||||
Class constructor to instatiate a Seller object
|
Class constructor to instatiate a customer object
|
||||||
|
|
||||||
|
No additional properties are assigned to the customer
|
||||||
'''
|
'''
|
||||||
def __init__(self):
|
def __init__(self, id: int, username: str, password: str, firstname: str,
|
||||||
super().__init__()
|
lastname: str, email: str, phone: str, role: str):
|
||||||
|
super().__init__(
|
||||||
|
id, username, password, firstname, lastname, email, phone, role
|
||||||
|
)
|
||||||
self.store = ""
|
self.store = ""
|
||||||
|
|
||||||
def login(self):
|
|
||||||
print("Logging in as Seller")
|
|
||||||
|
|
||||||
def signup(self):
|
|
||||||
print("Signing up as Seller")
|
|
||||||
|
|
||||||
def createProduct():
|
|
||||||
pass
|
|
||||||
|
|
||||||
def deleteProduct():
|
|
||||||
pass
|
|
||||||
|
|
||||||
def updateProduct():
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
@ -1,25 +1,15 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC
|
||||||
|
|
||||||
class User(ABC):
|
class User(ABC):
|
||||||
""" Functional Class constructor to initialise all properties in the base object
|
""" Functional Class constructor to initialise all properties in the base object
|
||||||
with a value """
|
with a value """
|
||||||
def __init__(self, id: int, username: str, email: str, firstname: str,
|
def __init__(self, id: int, username: str, password: str, firstname: str,
|
||||||
lastname: str, phone: str, password: str, role: str):
|
lastname: str, email: str, phone: str, role: str):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.username = username
|
self.username = username
|
||||||
self.email = email
|
self.password = password
|
||||||
self.firstName = firstname
|
self.firstName = firstname
|
||||||
self.lastName = lastname
|
self.lastName = lastname
|
||||||
|
self.email = email
|
||||||
self.phone = phone
|
self.phone = phone
|
||||||
self.password = password
|
|
||||||
self.role= role
|
self.role= role
|
||||||
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def login(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def signup(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
CREATE TABLE IF NOT EXISTS Users (
|
CREATE TABLE IF NOT EXISTS Users (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
|
username TEXT NOT NULL UNIQUE,
|
||||||
|
password TEXT NOT NULL,
|
||||||
first_name TEXT NOT NULL,
|
first_name TEXT NOT NULL,
|
||||||
last_name TEXT NOT NULL,
|
last_name TEXT NOT NULL,
|
||||||
email TEXT NOT NULL UNIQUE,
|
email TEXT NOT NULL UNIQUE,
|
||||||
phone TEXT NOT NULL UNIQUE,
|
phone TEXT NOT NULL,
|
||||||
password TEXT NOT NULL,
|
|
||||||
role TEXT NOT NULL
|
role TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -4,7 +4,11 @@
|
|||||||
<input type="text" name="search" placeholder="Find your favourite products" class="search-bar">
|
<input type="text" name="search" placeholder="Find your favourite products" class="search-bar">
|
||||||
<input type="submit" class="search-button">
|
<input type="submit" class="search-button">
|
||||||
</form>
|
</form>
|
||||||
<a href="login">Login/Signup</a>
|
{% if user != None: %}
|
||||||
|
<a href="logout">Welcome, {{ user }}</a>
|
||||||
|
{% else %}
|
||||||
|
<a href="login">Login/Signup</a>
|
||||||
|
{% endif %}
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<centre>
|
<centre>
|
||||||
|
Loading…
Reference in New Issue
Block a user