- Cleaned up the Nginx configuration for the Coolify deployment, ensuring proper formatting and organization. - Updated the `pyproject.toml` file to maintain consistent formatting. - Reformatted the `requirements.txt` file for better readability while keeping the dependencies unchanged.
58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
"""Auth blueprint — login, register, logout, index."""
|
|
from flask import Blueprint, flash, redirect, render_template, request, session, url_for
|
|
|
|
from ..helpers import _api
|
|
|
|
auth_bp = Blueprint("auth", __name__)
|
|
|
|
|
|
@auth_bp.get("/")
|
|
def index():
|
|
if "access_token" in session:
|
|
return redirect(url_for("dashboard.index"))
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
|
@auth_bp.route("/login", methods=["GET", "POST"])
|
|
def login():
|
|
if request.method == "POST":
|
|
email = request.form["email"]
|
|
password = request.form["password"]
|
|
resp = _api("POST", "/auth/login",
|
|
json={"email": email, "password": password})
|
|
if resp.status_code == 200:
|
|
data = resp.json()
|
|
session["access_token"] = data["access_token"]
|
|
session["refresh_token"] = data["refresh_token"]
|
|
me = _api("GET", "/users/me", token=data["access_token"])
|
|
if me.status_code == 200:
|
|
u = me.json()
|
|
session["user_email"] = u.get("email", "")
|
|
session["user_role"] = u.get("role", "user")
|
|
return redirect(url_for("dashboard.index"))
|
|
flash("Invalid email or password.", "error")
|
|
return render_template("login.html")
|
|
|
|
|
|
@auth_bp.route("/register", methods=["GET", "POST"])
|
|
def register():
|
|
if request.method == "POST":
|
|
email = request.form["email"]
|
|
password = request.form["password"]
|
|
resp = _api("POST", "/auth/register",
|
|
json={"email": email, "password": password})
|
|
if resp.status_code == 201:
|
|
flash("Account created. Please log in.", "success")
|
|
return redirect(url_for("auth.login"))
|
|
detail = resp.json().get("detail", "Registration failed.")
|
|
flash(detail, "error")
|
|
return render_template("register.html")
|
|
|
|
|
|
@auth_bp.get("/logout")
|
|
def logout():
|
|
refresh_token = session.get("refresh_token")
|
|
if refresh_token:
|
|
_api("POST", "/auth/logout", json={"refresh_token": refresh_token})
|
|
session.clear()
|
|
return redirect(url_for("auth.login")) |