- Implementado CRUDs para: cursos, empresas, anos_formaturas, tipos_servicos, tipos_eventos - Implementado lógica de precificação de eventos (precos_tipos_eventos) - Refatorado a autenticação: Simplificar o payload de cadastro/login (somente e-mail/senha), função padrão 'profissional' - Corrigido o middleware de autenticação: Resolvido a incompatibilidade de tipo UUID vs String (corrigir erro 500) - Aprimorado o Swagger: Adicionado structs nomeados, validação de duplicatas (409 Conflict) e segurança BearerAuth - Atualizar o esquema do banco de dados: Adicionar tabelas e restrições
40 lines
997 B
Go
40 lines
997 B
Go
package auth
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"photum-backend/internal/config"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func AuthMiddleware(cfg *config.Config) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
authHeader := c.GetHeader("Authorization")
|
|
if authHeader == "" {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "authorization header required"})
|
|
return
|
|
}
|
|
|
|
parts := strings.Split(authHeader, " ")
|
|
var tokenString string
|
|
if len(parts) == 2 && parts[0] == "Bearer" {
|
|
tokenString = parts[1]
|
|
} else if len(parts) == 1 && parts[0] != "" {
|
|
tokenString = parts[0]
|
|
} else {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid authorization header format"})
|
|
return
|
|
}
|
|
claims, err := ValidateToken(tokenString, cfg.JwtAccessSecret)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid token"})
|
|
return
|
|
}
|
|
|
|
c.Set("userID", claims.UserID.String())
|
|
c.Set("role", claims.Role)
|
|
c.Next()
|
|
}
|
|
}
|