modified: main_dc/docker-compose.yml

modified:   main_dc/yalarba/api_yal/cmd/main.go
	modified:   main_dc/yalarba/api_yal/internal/domain/auth/dto.go
	modified:   main_dc/yalarba/api_yal/internal/domain/auth/handler.go
	modified:   main_dc/yalarba/api_yal/internal/domain/auth/servcie.go
add AuthRes and UserInfo structs for request after auth
This commit is contained in:
2026-03-10 00:07:19 +05:00
parent 0108b981ce
commit 5561a9ee8c
5 changed files with 58 additions and 8 deletions
-2
View File
@@ -275,8 +275,6 @@ services:
build: build:
context: ./yalarba/api_yal # Укажите правильный путь к вашему проекту context: ./yalarba/api_yal # Укажите правильный путь к вашему проекту
dockerfile: Dockerfile dockerfile: Dockerfile
ports:
- "8787:8787"
container_name: api_yal container_name: api_yal
restart: unless-stopped restart: unless-stopped
env_file: env_file:
+3 -2
View File
@@ -63,9 +63,10 @@ func main() {
r := router.SetupRouter(db, cfg) r := router.SetupRouter(db, cfg)
// Создаем и запускаем сервер // Создаем и запускаем сервер
srv := server.NewServer(cfg.ServerPort, r) strServerPort := ":" + cfg.AppPort
srv := server.NewServer(strServerPort, r)
log.Printf("Server starting on port %s", ":8787") log.Printf("Server starting on port %s", strServerPort)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Server failed to start: %v", err) log.Fatalf("Server failed to start: %v", err)
} }
@@ -1,6 +1,6 @@
package auth package auth
import () import "time"
// RegisterRequest - запрос на регистрацию // RegisterRequest - запрос на регистрацию
type RegisterRequest struct { type RegisterRequest struct {
@@ -9,3 +9,27 @@ type RegisterRequest struct {
FirstName string `json:"first_name" validate:"required"` FirstName string `json:"first_name" validate:"required"`
LastName string `json:"last_name" validate:"required"` LastName string `json:"last_name" validate:"required"`
} }
// LoginRequest - запрос на вход
type LoginRequest struct {
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required"`
}
// AuthResponse структура ответа при успешной аутентификации
type AuthResponse struct {
Token string `json:"token"`
RefreshToken string `json:"refresh_token,omitempty"`
ExpiresAt time.Time `json:"expires_at"`
User UserInfo `json:"user"`
}
// UserInfo информация о пользователе для ответа
type UserInfo struct {
ID uint `json:"id"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
FullName string `json:"full_name"`
Role string `json:"role"`
}
@@ -6,6 +6,8 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"api_yal/internal/logger"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
) )
@@ -25,7 +27,8 @@ func NewAuthHandler(authService *AuthService) *AuthHandler {
// Register регистрация аккаунта пользователя // Register регистрация аккаунта пользователя
func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) { func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
l := logger.Get()
l.Debug("Регистрация нового пользователя AuthHandler")
var req RegisterRequest var req RegisterRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest) http.Error(w, "Invalid request body", http.StatusBadRequest)
@@ -52,4 +55,5 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
return return
} }
AuthService.Register(req)
} }
@@ -1,6 +1,13 @@
package auth package auth
import () import (
"errors"
"api_yal/internal/logger"
"api_yal/internal/models"
"golang.org/x/crypto/bcrypt"
)
type AuthService struct { type AuthService struct {
} }
@@ -9,3 +16,19 @@ func NewAuthService() *AuthService {
return &AuthService{ return &AuthService{
} }
} }
func (s *AuthService) Register(regReq RegisterRequest) (AuthResponse, error) {
l := logger.Get()
l.Debug("Регистрация пользователя AuthSerice")
// Хешируем пароль
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(regReq.Password), bcrypt.DefaultCost)
if err != nil {
return nil, err
}
newAcc := &models.Account{
Email: regReq.Email,
}
}