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:
@@ -275,8 +275,6 @@ services:
|
||||
build:
|
||||
context: ./yalarba/api_yal # Укажите правильный путь к вашему проекту
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- "8787:8787"
|
||||
container_name: api_yal
|
||||
restart: unless-stopped
|
||||
env_file:
|
||||
|
||||
@@ -63,9 +63,10 @@ func main() {
|
||||
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 {
|
||||
log.Fatalf("Server failed to start: %v", err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package auth
|
||||
|
||||
import ()
|
||||
import "time"
|
||||
|
||||
// RegisterRequest - запрос на регистрацию
|
||||
type RegisterRequest struct {
|
||||
@@ -9,3 +9,27 @@ type RegisterRequest struct {
|
||||
FirstName string `json:"first_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"
|
||||
"net/http"
|
||||
|
||||
"api_yal/internal/logger"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
@@ -25,7 +27,8 @@ func NewAuthHandler(authService *AuthService) *AuthHandler {
|
||||
|
||||
// Register регистрация аккаунта пользователя
|
||||
func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
l := logger.Get()
|
||||
l.Debug("Регистрация нового пользователя AuthHandler")
|
||||
var req RegisterRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
@@ -52,4 +55,5 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
AuthService.Register(req)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
package auth
|
||||
|
||||
import ()
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"api_yal/internal/logger"
|
||||
"api_yal/internal/models"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type AuthService struct {
|
||||
}
|
||||
@@ -8,4 +15,20 @@ type AuthService struct {
|
||||
func NewAuthService() *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,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user