mirror of
https://github.com/serty2005/rmser.git
synced 2026-02-05 03:12:34 -06:00
Перевел на multi-tenant
Добавил поставщиков Накладные успешно создаются из фронта
This commit is contained in:
49
internal/transport/http/middleware/auth.go
Normal file
49
internal/transport/http/middleware/auth.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"rmser/internal/domain/account"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AuthMiddleware извлекает Telegram User ID и находит User UUID
|
||||
func AuthMiddleware(accountRepo account.Repository) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 1. Ищем в заголовке (стандартный путь)
|
||||
tgIDStr := c.GetHeader("X-Telegram-User-ID")
|
||||
|
||||
// 2. Если нет в заголовке, ищем в Query (для отладки в браузере)
|
||||
// Пример: /api/drafts?_tg_id=12345678
|
||||
if tgIDStr == "" {
|
||||
tgIDStr = c.Query("_tg_id")
|
||||
}
|
||||
|
||||
if tgIDStr == "" {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Missing X-Telegram-User-ID header or _tg_id param"})
|
||||
return
|
||||
}
|
||||
|
||||
tgID, err := strconv.ParseInt(tgIDStr, 10, 64)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid Telegram ID"})
|
||||
return
|
||||
}
|
||||
|
||||
// Ищем пользователя в БД
|
||||
user, err := accountRepo.GetUserByTelegramID(tgID)
|
||||
if err != nil {
|
||||
// Если пользователя нет - значит он не нажал /start в боте
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "User not registered via Bot. Please start the bot first."})
|
||||
return
|
||||
}
|
||||
|
||||
// Кладем UUID пользователя в контекст
|
||||
c.Set("userID", user.ID)
|
||||
c.Set("telegramID", tgID)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user