add backend auth
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const UserIDKey = "userID"
|
||||
|
||||
func (s *AuthService) RequireAuth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
header := c.GetHeader("Authorization")
|
||||
if header == "" {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
||||
"error": gin.H{"code": "MISSING_TOKEN", "message": "authorization header required"},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
parts := strings.SplitN(header, " ", 2)
|
||||
if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
||||
"error": gin.H{"code": "INVALID_TOKEN", "message": "invalid authorization format"},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
claims, err := s.ValidateAccessToken(parts[1])
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
||||
"error": gin.H{"code": "INVALID_TOKEN", "message": "invalid or expired token"},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.Set(UserIDKey, claims)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user