Files
tp/main_dc/yalarba/api_yal/internal/middleware/authMiddleware.go
T
valitovgaziz 1e263cf1a2 new file: main_dc/yalarba/api_yal/internal/middleware/authMiddleware.go
modified:   main_dc/yalarba/api_yal/internal/router/router.go
add middleware for auth but empty for time
2026-03-09 02:02:48 +05:00

35 lines
1.1 KiB
Go

package middleware
import (
"context"
"net/http"
)
type contextKey string
const (
UserIDKey contextKey = "userID"
IsAuthKey contextKey = "isAuthenticated"
)
// AuthMiddlewareWithContext добавляет информацию об авторизации в контекст
func AuthMiddlewareWithContext(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Создаем контекст с тестовыми данными
ctx := r.Context()
ctx = context.WithValue(ctx, UserIDKey, 0)
ctx = context.WithValue(ctx, IsAuthKey, false)
// В реальном проекте здесь будет:
// token := r.Header.Get("Authorization")
// if token != "" {
// userID, err := validateToken(token)
// if err == nil {
// ctx = context.WithValue(ctx, UserIDKey, userID)
// ctx = context.WithValue(ctx, IsAuthKey, true)
// }
// }
next.ServeHTTP(w, r.WithContext(ctx))
})
}