Skip to content

Commit

Permalink
ref: auth middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
abbasfisal committed Dec 21, 2024
1 parent 3834ebb commit 6804e95
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions internal/middlewares/auth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package middlewares

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"shop/internal/database/mysql"
Expand All @@ -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()
Expand Down

0 comments on commit 6804e95

Please sign in to comment.