1e263cf1a2
modified: main_dc/yalarba/api_yal/internal/router/router.go add middleware for auth but empty for time
35 lines
1.1 KiB
Go
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))
|
|
})
|
|
} |