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.
28 lines
587 B
28 lines
587 B
1 month ago
|
package middleware
|
||
|
|
||
|
import (
|
||
1 month ago
|
"gofaster/internal/shared/pkg/auth"
|
||
1 month ago
|
"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()
|
||
|
}
|
||
|
}
|