modified: begushiybashkir/bbvue/src/stores/auth.js

modified:   begushiybashkir/bbvue/src/views/Register.vue
	modified:   serv_nginx/api_bb/internal/handlers/auth.go
	modified:   serv_nginx/api_bb/internal/models/user.go
	modified:   serv_nginx/api_bb/internal/service/auth_service.go
fix some register bags and set debag info loggers
This commit is contained in:
2025-10-10 04:48:42 +05:00
parent b7252c7900
commit 38c1e43ec2
5 changed files with 105 additions and 43 deletions
+60 -37
View File
@@ -3,13 +3,14 @@ package handlers
import (
"encoding/json"
"fmt"
"net/http"
"time"
"go-rest-api/internal/models"
"go-rest-api/internal/service"
"go-rest-api/pkg/middleware"
"go-rest-api/pkg/utils"
"go-rest-api/pkg/utils"
"github.com/go-chi/chi/v5"
)
@@ -29,17 +30,17 @@ func NewAuthHandler(authService service.AuthService, jwtService service.JWTServi
func (h *AuthHandler) Routes() chi.Router {
r := chi.NewRouter()
// Обработка OPTIONS запросов для CORS
// Обработка OPTIONS запросов для CORS
r.Options("/register", h.handleOptions)
r.Options("/login", h.handleOptions)
r.Options("/logout", h.handleOptions)
r.Options("/profile", h.handleOptions)
r.Post("/register", h.Register)
r.Post("/login", h.Login)
r.Post("/logout", h.Logout)
r.Get("/profile", h.GetProfile)
return r
}
@@ -51,16 +52,15 @@ func (h *AuthHandler) handleOptions(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
type RegisterRequest struct {
Email string `json:"email"`
Password string `json:"password"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Phone string `json:"phone"`
Experience string `json:"experience"`
Goals string `json:"goals"`
Newsletter bool `json:"newsletter"`
Email string `json:"email"`
Password string `json:"password"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Phone string `json:"phone"`
Experience string `json:"experience"`
Goals string `json:"goals"`
Newsletter bool `json:"newsletter"`
}
type LoginRequest struct {
@@ -69,28 +69,39 @@ type LoginRequest struct {
}
type UserResponse struct {
ID uint `json:"id"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Phone string `json:"phone"`
Experience string `json:"experience"`
Goals string `json:"goals"`
Newsletter bool `json:"newsletter"`
Role string `json:"role"`
ID uint `json:"id"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Phone string `json:"phone"`
Experience string `json:"experience"`
Goals string `json:"goals"`
Newsletter bool `json:"newsletter"`
Role string `json:"role"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Register request: %+v\n", r)
// Устанавливаем CORS заголовки
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
var req RegisterRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
utils.RespondWithError(w, http.StatusBadRequest, "Invalid request payload")
utils.RespondWithError(w, http.StatusBadRequest, "Invalid request payload: "+err.Error())
return
}
// Валидация обязательных полей
if req.FirstName == "" || req.LastName == "" || req.Email == "" || req.Password == "" {
utils.RespondWithError(w, http.StatusBadRequest, "First name, last name, email and password are required")
return
}
user := &models.User{
Email: req.Email,
Password: req.Password,
@@ -101,15 +112,19 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
Goals: req.Goals,
Newsletter: req.Newsletter,
Role: "user",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if err := h.authService.Register(user); err != nil {
utils.RespondWithError(w, http.StatusBadRequest, err.Error())
return
}
utils.RespondWithJSON(w, http.StatusCreated, map[string]string{
// После успешной регистрации возвращаем данные пользователя
utils.RespondWithJSON(w, http.StatusCreated, map[string]interface{}{
"message": "User registered successfully",
"user": toUserResponse(user),
})
}
@@ -117,30 +132,36 @@ func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
// Устанавливаем CORS заголовки
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
var req LoginRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
utils.RespondWithError(w, http.StatusBadRequest, "Invalid request payload")
utils.RespondWithError(w, http.StatusBadRequest, "Invalid request payload: "+err.Error())
return
}
// Валидация
if req.Email == "" || req.Password == "" {
utils.RespondWithError(w, http.StatusBadRequest, "Email and password are required")
return
}
user, token, err := h.authService.Login(req.Email, req.Password)
if err != nil {
utils.RespondWithError(w, http.StatusUnauthorized, err.Error())
return
}
// Устанавливаем токен в куки
http.SetCookie(w, &http.Cookie{
Name: "auth_token",
Value: token,
Path: "/",
HttpOnly: true,
Secure: false, // В production установить true :TODO
Secure: false, // В production установить true
SameSite: http.SameSiteLaxMode,
Expires: time.Now().Add(24 * time.Hour),
})
utils.RespondWithJSON(w, http.StatusOK, map[string]interface{}{
"message": "Login successful",
"token": token,
@@ -164,7 +185,7 @@ func (h *AuthHandler) Logout(w http.ResponseWriter, r *http.Request) {
Expires: time.Now().Add(-1 * time.Hour),
MaxAge: -1,
})
utils.RespondWithJSON(w, http.StatusOK, map[string]string{
"message": "Logout successful",
})
@@ -174,13 +195,13 @@ func (h *AuthHandler) GetProfile(w http.ResponseWriter, r *http.Request) {
// Устанавливаем CORS заголовки
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
user, ok := middleware.GetUserFromContext(r.Context())
if !ok {
utils.RespondWithError(w, http.StatusUnauthorized, "Authentication required")
return
}
utils.RespondWithJSON(w, http.StatusOK, toUserResponse(user))
}
@@ -195,5 +216,7 @@ func toUserResponse(user *models.User) UserResponse {
Goals: user.Goals,
Newsletter: user.Newsletter,
Role: user.Role,
CreatedAt: user.CreatedAt,
UpdatedAt: user.UpdatedAt,
}
}
}
+17
View File
@@ -37,4 +37,21 @@ func (u *User) HashPassword() error {
func (u *User) CheckPassword(password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
return err == nil
}
// BeforeCreate hook для GORM
func (u *User) BeforeCreate(tx *gorm.DB) error {
if u.CreatedAt.IsZero() {
u.CreatedAt = time.Now()
}
if u.UpdatedAt.IsZero() {
u.UpdatedAt = time.Now()
}
return u.HashPassword()
}
// BeforeUpdate hook для GORM
func (u *User) BeforeUpdate(tx *gorm.DB) error {
u.UpdatedAt = time.Now()
return nil
}
@@ -32,11 +32,28 @@ func (s *authService) Register(user *models.User) error {
if err == nil && existingUser != nil {
return errors.New("user with this email already exists")
}
// Хешируем пароль
if err := user.HashPassword(); err != nil {
return err
// Валидация обязательных полей
if user.FirstName == "" {
return errors.New("first name is required")
}
if user.LastName == "" {
return errors.New("last name is required")
}
if user.Email == "" {
return errors.New("email is required")
}
if user.Password == "" {
return errors.New("password is required")
}
if len(user.Password) < 6 {
return errors.New("password must be at least 6 characters")
}
// Хешируем пароль (будет вызван в BeforeCreate hook)
// if err := user.HashPassword(); err != nil {
// return err
// }
return s.userRepo.Create(user)
}