mirror of
				https://github.com/StefBuwalda/ProjectIOT.git
				synced 2025-10-30 11:19:57 +00:00 
			
		
		
		
	Added login functionality and login required
This commit is contained in:
		
							
								
								
									
										9
									
								
								application/auth/forms.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								application/auth/forms.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| from flask_wtf import FlaskForm | ||||
| from wtforms import StringField, SubmitField, PasswordField | ||||
| from wtforms.validators import DataRequired | ||||
|  | ||||
|  | ||||
| class login_form(FlaskForm): | ||||
|     username = StringField("Username", validators=[DataRequired()]) | ||||
|     password = PasswordField("Password", validators=[DataRequired()]) | ||||
|     submit = SubmitField(label="Sign in") | ||||
							
								
								
									
										19
									
								
								application/auth/models.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								application/auth/models.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| from application import db | ||||
| from flask_login import UserMixin | ||||
| from werkzeug.security import generate_password_hash, check_password_hash | ||||
|  | ||||
|  | ||||
| # User model | ||||
| class User(db.Model, UserMixin): | ||||
|     __tablename__ = "user" | ||||
|     id = db.Column(db.Integer, primary_key=True) | ||||
|     username = db.Column(db.String(150), unique=True, nullable=False) | ||||
|     password = db.Column(db.String, nullable=False) | ||||
|  | ||||
|     # Initialize user, prevents red stuff | ||||
|     def __init__(self, username: str, password: str, is_admin: bool = False): | ||||
|         self.username = username | ||||
|         self.password = generate_password_hash(password) | ||||
|  | ||||
|     def check_password(self, password: str): | ||||
|         return check_password_hash(self.password, password=password) | ||||
							
								
								
									
										85
									
								
								application/auth/templates/login.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								application/auth/templates/login.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,85 @@ | ||||
| <!DOCTYPE html> | ||||
| <html lang="en"> | ||||
|  | ||||
| <head> | ||||
|     <meta charset="UTF-8"> | ||||
|     <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||
|     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.5/dist/css/bootstrap.min.css" rel="stylesheet" | ||||
|         integrity="sha384-SgOJa3DmI69IUzQ2PVdRZhwQ+dy64/BUtbMJw1MZ8t5HZApcHrRKUc4W0kG879m7" crossorigin="anonymous"> | ||||
|     <title>Login</title> | ||||
|     <style> | ||||
|         .rounded-input { | ||||
|             border-radius: 20px; | ||||
|         } | ||||
|  | ||||
|         .split-background { | ||||
|             position: fixed; | ||||
|             top: 0; | ||||
|             left: 0; | ||||
|             width: 100%; | ||||
|             height: 50vh; | ||||
|             background-color: #424D66; | ||||
|             z-index: -1; | ||||
|         } | ||||
|  | ||||
|         @keyframes moveLeftRight { | ||||
|             0% { | ||||
|                 transform: translateX(0); | ||||
|             } | ||||
|  | ||||
|             100% { | ||||
|                 transform: translateX(1366px); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         .logo { | ||||
|             margin-top: 70px; | ||||
|             width: 770px; | ||||
|             height: auto; | ||||
|         } | ||||
|  | ||||
|         .car-image-container { | ||||
|             position: fixed; | ||||
|             bottom: 33px; | ||||
|         } | ||||
|  | ||||
|         .animate { | ||||
|             animation: moveLeftRight 15s infinite alternate; | ||||
|         } | ||||
|     </style> | ||||
| </head> | ||||
|  | ||||
| <body> | ||||
|     <div class="split-background"> | ||||
|         <div class="container-fluid text-center"> | ||||
|             <img src="{{ url_for('static', filename='images/logo-light.png') }}" alt="Logo" class="logo img-fluid mt-5"> | ||||
|         </div> | ||||
|     </div> | ||||
|     <div class="container d-flex justify-content-center align-items-center" style="min-height: 100vh;"> | ||||
|         <div class="col-md-6 col-lg-4"> | ||||
|             <form method="POST" class="p-5 border rounded-input shadow-sm bg-white bg-opacity-75"> | ||||
|                 {{form.hidden_tag()}} | ||||
|                 <div class=" p-4 border rounded-input shadow-sm bg-white"> | ||||
|                     <label for="username" class="form-label text-center w-100">Username</label> | ||||
|                     {{form.username(class="form-control rounded-input")}} | ||||
|                     <label for="password" class="form-label text-center w-100">Password</label> | ||||
|                     {{form.password(class="form-control rounded-input")}} | ||||
|                     <br> | ||||
|                     <div class="d-grid"> | ||||
|                         {{form.submit(class="btn btn-dark rounded-input px-4 mx-auto w-50")}} | ||||
|                     </div> | ||||
|                 </div> | ||||
|             </form> | ||||
|         </div> | ||||
|     </div> | ||||
|     <div class="car-image-container"> | ||||
|         <img src="{{ url_for('static', filename='images/car.png') }}" alt="Moving Image" class="animate"> | ||||
|     </div> | ||||
|     <footer class="py-3 bg-dark text-white fixed-bottom""> | ||||
|                 <div class=" container text-center"> | ||||
|         <span class="text-muted"> </span> | ||||
|         </div> | ||||
|     </footer> | ||||
| </body> | ||||
|  | ||||
| </html> | ||||
| @@ -1,23 +1,46 @@ | ||||
| from flask import Blueprint, session, redirect, url_for, render_template | ||||
| from flask import Blueprint, redirect, url_for, render_template | ||||
| from application.auth.forms import login_form | ||||
| from application.auth.models import User | ||||
| from flask_login import login_user, login_required, logout_user | ||||
|  | ||||
| # from application import keycloak | ||||
|  | ||||
| auth_blueprint = Blueprint("auth", __name__, template_folder="templates") | ||||
|  | ||||
|  | ||||
| @auth_blueprint.route("/demo") | ||||
| def demo(): | ||||
|     return render_template("login.html") | ||||
| # Login as user or admin | ||||
| @auth_blueprint.route("/login", methods=["GET", "POST"]) | ||||
| def login(): | ||||
|     loginForm = login_form() | ||||
|     if loginForm.validate_on_submit(): | ||||
|         print("Test2") | ||||
|         username = loginForm.username.data | ||||
|         password = loginForm.password.data | ||||
|         user = User.query.filter_by(username=username).first() | ||||
|  | ||||
|         if user and user.check_password(password=password): | ||||
|             login_user(user) | ||||
|             return redirect(url_for("dash.dashboard")) | ||||
|     return render_template("login.html", form=loginForm) | ||||
|  | ||||
|  | ||||
| @auth_blueprint.route("/") | ||||
| def home(): | ||||
|     user = session.get("user") | ||||
|     if user: | ||||
|         return f'Hello, {user["name"]}' | ||||
|     return redirect(url_for("auth.login")) | ||||
| @auth_blueprint.route("/logout") | ||||
| @login_required | ||||
| def logout(): | ||||
|     logout_user() | ||||
|     return redirect("/") | ||||
|  | ||||
|  | ||||
| """ | ||||
| # Logout | ||||
| @auth_blueprint.route("/logout") | ||||
| @login_required | ||||
| def logout(): | ||||
|     logout_user() | ||||
|     flash("Logged out succesfully") | ||||
|     return redirect(url_for("index")) | ||||
| """ | ||||
|  | ||||
| """ | ||||
| @auth_blueprint.route("/login") | ||||
| def login(): | ||||
|   | ||||
		Reference in New Issue
	
	Block a user