diff --git a/internal/middlewares/auth.go b/internal/middlewares/auth.go index dd51880..d119f78 100644 --- a/internal/middlewares/auth.go +++ b/internal/middlewares/auth.go @@ -1,7 +1,6 @@ package middlewares import ( - "fmt" "github.com/gin-gonic/gin" "net/http" "shop/internal/database/mysql" @@ -11,23 +10,26 @@ import ( ) func IsAdmin(c *gin.Context) { - fmt.Println("admin middleware") authID := sessions.GET(c, "auth_id") - if authID == "" { - fmt.Println("auth id not found in admin middleware") + c.Redirect(http.StatusFound, "/admins/login") + return + } + + userID, err := strconv.Atoi(authID) + if err != nil { c.Redirect(http.StatusFound, "/admins/login") return } repo := adminAuthRepo.NewAuthenticateRepository(mysql.Get()) - userID, _ := strconv.Atoi(authID) - user, _ := repo.FindByUserID(c, uint(userID)) + user, err := repo.FindByUserID(c, uint(userID)) - if user.ID <= 0 || user.Type != "admin" { + if err != nil || user == nil || user.ID <= 0 || user.Type != "admin" { c.Redirect(http.StatusFound, "/admins/login") + return } c.Next()