2026-04-09 10:01:23 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-31 18:43:48 +08:00
|
|
|
"agent-desk/internal/pkg/i18nx"
|
|
|
|
|
"agent-desk/internal/services"
|
2026-04-09 10:01:23 +08:00
|
|
|
|
2026-05-23 22:10:20 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2026-04-09 10:01:23 +08:00
|
|
|
"github.com/mlogclub/simple/web"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-23 22:10:20 +08:00
|
|
|
func AuthMiddleware(ctx *gin.Context) {
|
2026-04-09 10:01:23 +08:00
|
|
|
if !authenticateRequest(ctx) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Next()
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:10:20 +08:00
|
|
|
func authenticateRequest(ctx *gin.Context) bool {
|
2026-04-09 10:01:23 +08:00
|
|
|
if _, err := services.AuthService.Authenticate(ctx); err != nil {
|
2026-05-25 12:06:15 +08:00
|
|
|
result := web.JsonError(err)
|
2026-06-02 20:51:13 +08:00
|
|
|
result.Message = i18nx.T(ctx, "error.auth.expired")
|
2026-05-25 12:06:15 +08:00
|
|
|
ctx.JSON(200, result)
|
2026-05-23 22:10:20 +08:00
|
|
|
ctx.Abort()
|
2026-04-09 10:01:23 +08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|