-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.py
70 lines (57 loc) · 2.31 KB
/
authentication.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from flask import Blueprint, render_template, request, redirect, url_for, jsonify, Response
from flask_login import login_user, logout_user, current_user, login_required
from database import get_user, save_user
authentication: Blueprint = Blueprint("authentication", __name__, url_prefix="/authentication")
@authentication.route(rule="/login/", methods=["GET", "POST"])
def login() -> Response | str:
"""
Manages the login operation logic, handling both GET and POST requests,
and redirects authenticated users to the home page.
:return: HTML Template or Response.
"""
if current_user.is_authenticated:
return redirect(url_for("home"))
if request.method == "POST":
data = request.get_json(force=True)
username = data.get("username")
password = data.get("password")
user_object = get_user(username) # get the user object by username
if user_object and user_object.check_password(password_input=password):
login_user(user_object)
response_data = {
"status": True,
"message": "Login Success!",
"redirectUrl": url_for("home")
}
else:
response_data = {
"status": False,
"message": "Bad Credentials"
}
return jsonify(response_data)
return render_template("login.html")
@authentication.route(rule="/register/", methods=["GET", "POST"])
def register() -> Response | str:
"""
Handles the registration operation logic, including GET and POST requests,
and redirects authenticated users to the home page.
:return: HTML Template or Response.
"""
if current_user.is_authenticated:
return redirect(url_for("home"))
if request.method == "POST":
data = request.get_json(force=True)
username = data.get("username")
email = data.get("email")
password_input = data.get("password")
return save_user(username=username, email=email, password=password_input)
return render_template("register.html")
@authentication.route(rule="/logout")
@login_required
def logout() -> Response:
"""
Logs out the user and redirects them to the home page; requires user authentication.
:return: Response
"""
logout_user()
return redirect(url_for("home"))