32 lines
963 B
Python
32 lines
963 B
Python
|
|
from flask import Blueprint, request, render_template, redirect, flash, session
|
||
|
|
from app.services.user_service import UserService
|
||
|
|
|
||
|
|
auth_bp = Blueprint("auth", __name__, url_prefix="/auth")
|
||
|
|
|
||
|
|
# LOGIN PAGE
|
||
|
|
@auth_bp.route("/login", methods=["GET", "POST"])
|
||
|
|
def login():
|
||
|
|
if request.method == "POST":
|
||
|
|
user = UserService.validate_login(
|
||
|
|
request.form["email"],
|
||
|
|
request.form["password"]
|
||
|
|
)
|
||
|
|
if user:
|
||
|
|
session["user_id"] = user.id
|
||
|
|
return redirect("/dashboard")
|
||
|
|
flash("Invalid credentials", "danger")
|
||
|
|
return render_template("login.html")
|
||
|
|
|
||
|
|
# REGISTER API ONLY
|
||
|
|
@auth_bp.route("/register", methods=["POST"])
|
||
|
|
def register():
|
||
|
|
data = request.json
|
||
|
|
UserService.register_user(data["name"], data["email"], data["password"])
|
||
|
|
return {"message": "User registered successfully"}, 201
|
||
|
|
|
||
|
|
# LOGOUT
|
||
|
|
@auth_bp.route("/logout")
|
||
|
|
def logout():
|
||
|
|
session.clear()
|
||
|
|
return redirect("/auth/login")
|