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

@@ -2,6 +2,7 @@ package app
import ( import (
"mal/internal/database" "mal/internal/database"
"mal/internal/auth"
"mal/internal/server" "mal/internal/server"
"mal/internal/templates" "mal/internal/templates"
@@ -14,6 +15,7 @@ func NewApp() *fx.App {
return fx.New( return fx.New(
database.Module, database.Module,
jikan.Module, jikan.Module,
auth.Module,
templates.Module, templates.Module,
server.Module, server.Module,
fx.Decorate(func(r *templates.Renderer) render.HTMLRender { fx.Decorate(func(r *templates.Renderer) render.HTMLRender {

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")
}

23
internal/auth/module.go Normal file
View File

@@ -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
}),
),
)

View File

@@ -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)
}

View File

@@ -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)
}

23
internal/domain/auth.go Normal file
View File

@@ -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
}