You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
571 B
27 lines
571 B
package middleware |
|
|
|
import ( |
|
"gofaster/pkg/auth" |
|
"net/http" |
|
|
|
"github.com/gin-gonic/gin" |
|
) |
|
|
|
func AuthMiddleware(jwtSecret string) gin.HandlerFunc { |
|
return func(c *gin.Context) { |
|
token := c.GetHeader("Authorization") |
|
if token == "" { |
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "未提供认证令牌"}) |
|
return |
|
} |
|
|
|
claims, err := auth.ParseToken(token, jwtSecret) |
|
if err != nil { |
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "无效的认证令牌"}) |
|
return |
|
} |
|
|
|
c.Set("user_id", claims.UserID) |
|
c.Next() |
|
} |
|
}
|
|
|