Repository navigation
Expand file tree
/
Copy pathauth.py
More file actions
46 lines (34 loc) · 1.47 KB
/
Copy pathauth.py
File metadata and controls
46 lines (34 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from flask import Blueprint, request, render_template, redirect, url_for, flash, send_from_directory
from flask_login import login_user, logout_user, login_required, current_user
from werkzeug.security import check_password_hash
from models import User
auth_bp = Blueprint("auth", __name__)
@auth_bp.route("/login", methods=["GET"])
def login_page():
if current_user.is_authenticated:
return redirect(url_for("main.dashboard"))
return render_template("login.html")
@auth_bp.route("/login", methods=["GET", "POST"])
def login():
if current_user.is_authenticated:
return redirect(url_for("main.dashboard"))
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
user = User.query.filter_by(username=username).first()
if user and check_password_hash(user.password, password):
login_user(user)
return redirect(url_for("main.dashboard"))
else:
flash("Invalid username or password", "danger")
return render_template("login.html")
@auth_bp.route("/logout", methods=["POST"])
@login_required
def logout():
logout_user()
flash("You have been logged out.", "success")
return redirect(url_for("auth.login_page"))
@auth_bp.route('/logo')
def serve_logo():
# This is a hack - not recommended for production
return send_from_directory('templates', 'CIIT-College-logo-negative.png')