From 34aeb91252ae95739b2be77a6a0d8db27d6c5028 Mon Sep 17 00:00:00 2001 From: mkelvers Date: Wed, 13 May 2026 10:31:46 +0200 Subject: [PATCH] feat: migrate auth module to modular domain pattern --- internal/app/app.go | 2 + internal/auth/handler/handler.go | 58 ++++++++++++++++++++++ internal/auth/module.go | 23 +++++++++ internal/auth/repository/repository.go | 69 ++++++++++++++++++++++++++ internal/auth/service/service.go | 57 +++++++++++++++++++++ internal/domain/auth.go | 23 +++++++++ 6 files changed, 232 insertions(+) create mode 100644 internal/auth/handler/handler.go create mode 100644 internal/auth/module.go create mode 100644 internal/auth/repository/repository.go create mode 100644 internal/auth/service/service.go create mode 100644 internal/domain/auth.go diff --git a/internal/app/app.go b/internal/app/app.go index 61bc9e0..1c7bb17 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -2,6 +2,7 @@ package app import ( "mal/internal/database" + "mal/internal/auth" "mal/internal/server" "mal/internal/templates" @@ -14,6 +15,7 @@ func NewApp() *fx.App { return fx.New( database.Module, jikan.Module, + auth.Module, templates.Module, server.Module, fx.Decorate(func(r *templates.Renderer) render.HTMLRender { diff --git a/internal/auth/handler/handler.go b/internal/auth/handler/handler.go new file mode 100644 index 0000000..1474819 --- /dev/null +++ b/internal/auth/handler/handler.go @@ -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") +} diff --git a/internal/auth/module.go b/internal/auth/module.go new file mode 100644 index 0000000..c9ea02c --- /dev/null +++ b/internal/auth/module.go @@ -0,0 +1,23 @@ +package auth + +import ( + "mal/internal/auth/handler" + "mal/internal/auth/repository" + "mal/internal/auth/service" + "mal/internal/server" + + "go.uber.org/fx" +) + +var Module = fx.Options( + fx.Provide( + repository.NewAuthRepository, + service.NewAuthService, + handler.NewAuthHandler, + ), + fx.Provide( + server.AsRouteRegister(func(h *handler.AuthHandler) server.RouteRegister { + return h + }), + ), +) diff --git a/internal/auth/repository/repository.go b/internal/auth/repository/repository.go new file mode 100644 index 0000000..b41f241 --- /dev/null +++ b/internal/auth/repository/repository.go @@ -0,0 +1,69 @@ +package repository + +import ( + "context" + "database/sql" + "errors" + "mal/internal/db" + "mal/internal/domain" + "time" + + "github.com/google/uuid" +) + +type authRepository struct { + queries *db.Queries +} + +func NewAuthRepository(queries *db.Queries) domain.AuthRepository { + return &authRepository{queries: queries} +} + +func (r *authRepository) GetUserByUsername(ctx context.Context, username string) (*domain.User, error) { + u, err := r.queries.GetUserByUsername(ctx, username) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + return nil, err + } + return &u, nil +} + +func (r *authRepository) GetUserByID(ctx context.Context, id string) (*domain.User, error) { + u, err := r.queries.GetUser(ctx, id) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + return nil, err + } + return &u, nil +} + +func (r *authRepository) CreateSession(ctx context.Context, userID string, sessionID string) (*domain.Session, error) { + s, err := r.queries.CreateSession(ctx, db.CreateSessionParams{ + ID: sessionID, + UserID: userID, + ExpiresAt: time.Now().Add(24 * time.Hour), + }) + if err != nil { + return nil, err + } + return &s, nil +} + +func (r *authRepository) GetSession(ctx context.Context, sessionID string) (*domain.Session, error) { + s, err := r.queries.GetSession(ctx, sessionID) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + return nil, err + } + return &s, nil +} + +func (r *authRepository) DeleteSession(ctx context.Context, sessionID string) error { + return r.queries.DeleteSession(ctx, sessionID) +} diff --git a/internal/auth/service/service.go b/internal/auth/service/service.go new file mode 100644 index 0000000..249140c --- /dev/null +++ b/internal/auth/service/service.go @@ -0,0 +1,57 @@ +package service + +import ( + "context" + "errors" + "mal/internal/domain" + "time" + + "github.com/google/uuid" + "golang.org/x/crypto/bcrypt" +) + +type authService struct { + repo domain.AuthRepository +} + +func NewAuthService(repo domain.AuthRepository) domain.AuthService { + return &authService{repo: repo} +} + +func (s *authService) Login(ctx context.Context, username, password string) (*domain.Session, error) { + user, err := s.repo.GetUserByUsername(ctx, username) + if err != nil { + return nil, err + } + if user == nil { + return nil, errors.New("invalid credentials") + } + + if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password)); err != nil { + return nil, errors.New("invalid credentials") + } + + sessionID := uuid.New().String() + return s.repo.CreateSession(ctx, user.ID, sessionID) +} + +func (s *authService) ValidateSession(ctx context.Context, sessionID string) (*domain.User, error) { + session, err := s.repo.GetSession(ctx, sessionID) + if err != nil { + return nil, err + } + if session == nil { + return nil, errors.New("session not found") + } + + if session.ExpiresAt.Before(time.Now()) { + _ = s.repo.DeleteSession(ctx, sessionID) + return nil, errors.New("session expired") + } + + return s.repo.GetUserByID(ctx, session.UserID) +} + +func (s *authService) Logout(ctx context.Context, sessionID string) error { + return s.repo.DeleteSession(ctx, sessionID) +} diff --git a/internal/domain/auth.go b/internal/domain/auth.go new file mode 100644 index 0000000..2f38fbb --- /dev/null +++ b/internal/domain/auth.go @@ -0,0 +1,23 @@ +package domain + +import ( + "context" + "mal/internal/db" +) + +type User = db.User +type Session = db.Session + +type AuthService interface { + Login(ctx context.Context, username, password string) (*Session, error) + ValidateSession(ctx context.Context, sessionID string) (*User, error) + Logout(ctx context.Context, sessionID string) error +} + +type AuthRepository interface { + GetUserByUsername(ctx context.Context, username string) (*User, error) + GetUserByID(ctx context.Context, id string) (*User, error) + CreateSession(ctx context.Context, userID string, sessionID string) (*Session, error) + GetSession(ctx context.Context, sessionID string) (*Session, error) + DeleteSession(ctx context.Context, sessionID string) error +}