Change logging to be json comaptible
This commit is contained in:
@@ -2,6 +2,9 @@ package auth
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"ResendIt/internal/api/middleware"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -37,16 +40,44 @@ func (h *Handler) Login(c *gin.Context) {
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
log := middleware.StructuredLog(c)
|
||||
|
||||
log.Warn().
|
||||
Str("event", "login_failed").
|
||||
Str("reason", "invalid_request").
|
||||
Str("username", req.Username).
|
||||
Msg("Login attempt with invalid request")
|
||||
|
||||
c.JSON(400, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
log := middleware.StructuredLog(c).With().
|
||||
Str("event", "login_attempt").
|
||||
Str("username", req.Username).
|
||||
Str("ip", c.ClientIP()).
|
||||
Logger()
|
||||
|
||||
start := time.Now()
|
||||
token, err := h.service.Login(req.Username, req.Password)
|
||||
latency := time.Since(start)
|
||||
|
||||
if err != nil {
|
||||
log.Warn().
|
||||
Str("result", "failed").
|
||||
Dur("latency_ms", latency).
|
||||
Err(err).
|
||||
Msg("Login failed")
|
||||
|
||||
c.JSON(401, gin.H{"error": "Invalid credentials"})
|
||||
return
|
||||
}
|
||||
|
||||
log.Info().
|
||||
Str("result", "success").
|
||||
Dur("latency_ms", latency).
|
||||
Msg("Login successful")
|
||||
|
||||
isSecure := os.Getenv("USE_HTTPS") == "true"
|
||||
|
||||
c.SetCookie(
|
||||
@@ -56,7 +87,7 @@ func (h *Handler) Login(c *gin.Context) {
|
||||
"/",
|
||||
os.Getenv("DOMAIN"),
|
||||
isSecure,
|
||||
true, // httpOnly (IMPORTANT)
|
||||
true,
|
||||
)
|
||||
|
||||
c.JSON(200, gin.H{"token": token})
|
||||
|
||||
Reference in New Issue
Block a user