feat: migrate auth module to modular domain pattern

This commit is contained in:
2026-05-13 10:31:46 +02:00
parent afdd880d0e
commit 34aeb91252
6 changed files with 232 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
package handler
import (
"mal/internal/domain"
"mal/internal/server"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
type AuthHandler struct {
svc domain.AuthService
}
func NewAuthHandler(svc domain.AuthService) *AuthHandler {
return &AuthHandler{svc: svc}
}
func (h *AuthHandler) Register(r *gin.Engine) {
r.GET("/login", h.HandleLoginPage)
r.POST("/login", h.HandleLogin)
r.GET("/logout", h.HandleLogout)
}
func (h *AuthHandler) HandleLoginPage(c *gin.Context) {
c.HTML(http.StatusOK, "login.gohtml", gin.H{
"CurrentPath": "/login",
})
}
func (h *AuthHandler) HandleLogin(c *gin.Context) {
username := c.PostForm("username")
password := c.PostForm("password")
session, err := h.svc.Login(c.Request.Context(), username, password)
if err != nil {
c.HTML(http.StatusUnauthorized, "login.gohtml", gin.H{
"Error": "Invalid username or password",
"CurrentPath": "/login",
})
return
}
c.SetCookie("session_id", session.ID, int(24*time.Hour.Seconds()), "/", "", false, true)
c.Header("HX-Redirect", "/")
c.Redirect(http.StatusSeeOther, "/")
}
func (h *AuthHandler) HandleLogout(c *gin.Context) {
sessionID, err := c.Cookie("session_id")
if err == nil {
_ = h.svc.Logout(c.Request.Context(), sessionID)
}
c.SetCookie("session_id", "", -1, "/", "", false, true)
c.Redirect(http.StatusSeeOther, "/login")
}