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
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
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))
|
||||
})
|
||||
}
|
||||
@@ -23,6 +23,8 @@ func SetupRouter(db *gorm.DB, config *config.Config) http.Handler {
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "healthy"})
|
||||
})
|
||||
|
||||
r.Group()
|
||||
|
||||
zapLogger.Info("End setup routers")
|
||||
|
||||
// Логируем все зарегистрированные маршруты
|
||||
|
||||
Reference in New Issue
Block a user