mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-04 18:52:56 +00:00
34 lines
772 B
Go
34 lines
772 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"fotbal-club/internal/models"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// AdminMiddleware checks if the user has admin role
|
|
func AdminMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
user, exists := c.Get("user")
|
|
if !exists {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
|
return
|
|
}
|
|
|
|
// Type assert the user to your User model
|
|
userModel, ok := user.(*models.User)
|
|
if !ok {
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Internal server error"})
|
|
return
|
|
}
|
|
|
|
// Check if user is admin
|
|
if userModel.Role != "admin" {
|
|
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "Forbidden - Admin access required"})
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|