Files
tp/serv_nginx/api_bb/internal/service/auth_service.go
T

52 lines
1.5 KiB
Go

package service
import (
"errors"
"go-rest-api/internal/models"
"go-rest-api/internal/repository"
)
type AuthService interface {
Register(user *models.User) error
Login(email, password string) (*models.User, error)
}
type authService struct {
userRepo repository.UserRepository
}
func NewAuthService(userRepo repository.UserRepository) AuthService {
return &authService{userRepo: userRepo}
}
func (s *authService) Register(user *models.User) error {
// Проверка существования пользователя
existingUser, _ := s.userRepo.FindByEmail(user.Email)
if existingUser != nil {
return errors.New("user already exists")
}
// Здесь должна быть хеширование пароля
// user.Password = hashPassword(user.Password)
return s.userRepo.Create(user)
}
func (s *authService) Login(email, password string) (*models.User, error) {
user, err := s.userRepo.FindByEmail(email)
if err != nil {
return nil, errors.New("invalid credentials")
}
// Здесь должна быть проверка хеша пароля
// if !checkPasswordHash(password, user.Password) {
// return nil, errors.New("invalid credentials")
// }
// Временно просто проверяем напрямую (для демо)
if user.Password != password {
return nil, errors.New("invalid credentials")
}
return user, nil
}