Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated version of auth #18

Open
wants to merge 1 commit into
base: K1rk3y-Backend
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion project/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager

db = SQLAlchemy()
login_manager = LoginManager()

def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'

db.init_app(app)
login_manager.init_app(app)

with app.app_context():
from . import model # Import models
Expand All @@ -24,4 +27,11 @@ def create_app():
app.register_blueprint(submit_bp)
app.register_blueprint(track_bp)

return app
from .model import User # Import your User model

@login_manager.user_loader
def load_user(user_id):
# Load and return the User object
return User.query.get(int(user_id))

return app
Binary file modified project/app/auth/__pycache__/routes.cpython-310.pyc
Binary file not shown.
70 changes: 41 additions & 29 deletions project/app/auth/routes.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,65 @@
from flask import Blueprint, render_template, session, request, flash, redirect, url_for
from ..util import record_activity
from ..form_class import LoginForm, RegistrationForm
from flask import Blueprint, render_template, session, request, flash, redirect, url_for, jsonify
from flask_login import login_user, logout_user, current_user, login_required
from werkzeug.security import check_password_hash, generate_password_hash
from ..form_class import LoginForm, RegistrationForm
from ..model import UserActivity, User
from .. import db

auth_bp = Blueprint('auth', __name__)

@auth_bp.route('/login', methods=['GET', 'POST'])
@auth_bp.route('/login')
def login():
return render_template('login.html', title='Sign In')


@auth_bp.route('/login-cook', methods=['GET', 'POST'])
def login_cook():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user and check_password_hash(user.password, form.password.data):
login_user(user) # Log in the user
flash('Logged in successfully', 'success')
return redirect(url_for('form.index'))
else:
flash('Invalid username or password', 'error')
return render_template('login-cook.html', title='Sign In', form=form)


@auth_bp.route('/login-patron', methods=['GET', 'POST'])
def login_patron():
form = LoginForm()
if request.method == 'POST':
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user and check_password_hash(user.password, form.password.data):
session['user_id'] = user.id # Store user's ID in session
session['username'] = form.username.data # Set username in session
flash('Logged in successfully', 'success')
return redirect(url_for('auth.login_success'))
else:
flash('Invalid username or password', 'error')
return render_template('login.html', title='Sign In', form=form)


@auth_bp.route('/login_success')
def login_success():
if 'username' in session and 'user_id' in session:
return f"Welcome {session['username']}! Login successful."
return redirect(url_for('auth.login'))
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user and check_password_hash(user.password, form.password.data):
login_user(user) # Log in the user
flash('Logged in successfully', 'success')
return redirect(url_for('form.index'))
else:
flash('Invalid username or password', 'error')
return render_template('login-patron.html', title='Sign In', form=form)


@auth_bp.route('/register', methods=['GET', 'POST'])
def register():
if 'username' in session and 'user_id' in session:
return f"{session['username']}, you are already logged in. Log out to register."
if current_user.is_authenticated:
return f"{current_user.username}, you are already logged in. Log out to register."

form = RegistrationForm()
if form.validate_on_submit():
user = User(username=form.username.data, password=generate_password_hash(form.password.data))
db.session.add(user)
db.session.commit()
flash('Your account has been created! You are now able to log in', 'success')
return redirect(url_for('auth.login')) # Assuming the login route is named 'subfolder.login'
return render_template('register.html', title='Register', form=form)
flash_message = 'Your account has been created! You are now able to log in'
flash(flash_message, 'success')
return redirect(url_for('auth.login')) # Redirect to the login page after successful registration

return render_template('sign-up.html', title='Register', form=form, flash_message=None)


@auth_bp.route('/logout')
@login_required
def logout():
session.pop('username', None)
session.pop('user_id', None)
logout_user() # Log out the current user
flash('You have been logged out.', 'info')
return redirect(url_for('auth.login'))
32 changes: 30 additions & 2 deletions project/app/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from . import db
from datetime import datetime, timezone
from flask_login import UserMixin

class UserActivity(db.Model):
id = db.Column(db.Integer, primary_key=True)
Expand All @@ -8,7 +9,34 @@ class UserActivity(db.Model):
timestamp = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))


class User(db.Model):
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password = db.Column(db.String(120), nullable=False)
password = db.Column(db.String(120), nullable=False)
first_name = db.Column(db.String(64), nullable=False)
last_name = db.Column(db.String(64), nullable=False)
address = db.Column(db.String(128), nullable=False)
suburb = db.Column(db.String(64))
email = db.Column(db.String(120), unique=True, nullable=False)
phone = db.Column(db.String(15), nullable=False)
meal = db.Column(db.String(64), nullable=False)
number_of_people = db.Column(db.Integer, nullable=False)
instructions = db.Column(db.String(200))

# Implementing the required methods and properties for Flask-Login
def get_id(self):
return str(self.id)

@property
def is_authenticated(self):
# Define your logic for user authentication here, e.g., check if the user is authenticated
return True # For example, always return True if authentication is not implemented

@property
def is_active(self):
# Define your logic to determine if the user is active or not
return True # For example, always return True if all users are considered active

@property
def is_anonymous(self):
return False # Assuming users are not anonymous in your system
Binary file added project/app/static/image/bb.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added project/app/static/image/dal.webp
Binary file not shown.
Binary file added project/app/static/image/dal_blurred.webp
Binary file not shown.
Binary file added project/app/static/image/header.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added project/app/static/image/hh.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added project/app/static/image/img.webp
Binary file not shown.
Binary file added project/app/static/image/menu-btn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added project/app/static/image/moving.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added project/app/static/image/moving2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added project/app/static/image/new.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added project/app/static/image/topSeller.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
130 changes: 130 additions & 0 deletions project/app/static/login.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* styles.css */
body, html {
height: 100%;
margin: 0;
font-family: Arial, sans-serif;
}

.container {
width: 100%;
height: 100%;
background-color: #899181;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: white;
}


.header {
margin-bottom: 20px;
}

.logo {
width: 200px;
}

.login-form {
width: 300px;
text-align: center;
}

.login-btn {
width: 100%;
padding: 10px;
margin-bottom: 10px;
font-size: 16px;
border: none;
background-color: #f1f1f1;
color: #333;
cursor: pointer;
}

.login-btn:hover {
background-color: #ddd;
}

.footer {
margin-top: 20px;
}

.footer-link {
color: white;
text-decoration: none;
margin: 0 10px;
}

.footer-link:hover {
text-decoration: underline;
}

input[type="text"], input[type="password"], .dropbtn {
width: 100%; /* Ensures full-width in container */
padding: 15px; /* Uniform padding for visual consistency */
margin: 5px 0 22px 0; /* Margin for spacing */
display: inline-block;
border: 1px solid #ccc; /* Adding border for consistency */
background: #f1f1f1; /* Background color for default state */
font-size: 16px; /* Font size matched with other inputs */
border-radius: 4px; /* Adding rounded corners for aesthetics */
}

input[type="text"]:focus, input[type="password"]:focus, .dropbtn:focus {
background-color: #ddd; /* Focus background color */
outline: none; /* Removes the outline */
border: 1px solid #888; /* Darker border on focus for better visibility */
}

hr {
border: 1px solid #f1f1f1;
margin-bottom: 25px;
}

/* The container <div> for dropdown - adjusted for correct positioning */
.dropdown {
position: relative;
display: block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 100%; /* Match the width of the dropdown button */
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu on click */
.dropdown .dropbtn:focus + .dropdown-content {
display: block;
}

.registerbtn {
background-color: #4CAF50;
color: white;
padding: 16px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 20%; /* Changed to full width */
opacity: 0.9;
border-radius: 4px; /* Consistent rounded corners */
}

.registerbtn:hover {
opacity:1;
}
Loading