Update template go
This commit is contained in:
@@ -197,30 +197,30 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
// "github.com/gin-gonic/gin"
|
||||
// )
|
||||
|
||||
// // AuthMiddleware validates Bearer token in Authorization header
|
||||
// func AuthMiddleware() gin.HandlerFunc {
|
||||
// return func(c *gin.Context) {
|
||||
// authHeader := c.GetHeader("Authorization")
|
||||
// if authHeader == "" {
|
||||
// c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Authorization header missing"})
|
||||
// return
|
||||
// }
|
||||
// AuthMiddleware validates Bearer token in Authorization header
|
||||
func AuthJWTMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Authorization header missing"})
|
||||
return
|
||||
}
|
||||
|
||||
// parts := strings.SplitN(authHeader, " ", 2)
|
||||
// if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
|
||||
// c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Authorization header format must be Bearer {token}"})
|
||||
// return
|
||||
// }
|
||||
parts := strings.SplitN(authHeader, " ", 2)
|
||||
if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Authorization header format must be Bearer {token}"})
|
||||
return
|
||||
}
|
||||
|
||||
// token := parts[1]
|
||||
// // For now, use a static token for validation. Replace with your logic.
|
||||
// const validToken = "your-static-token"
|
||||
token := parts[1]
|
||||
// For now, use a static token for validation. Replace with your logic.
|
||||
const validToken = "your-static-token"
|
||||
|
||||
// if token != validToken {
|
||||
// c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
||||
// return
|
||||
// }
|
||||
if token != validToken {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
||||
return
|
||||
}
|
||||
|
||||
// c.Next()
|
||||
// }
|
||||
// }
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user