turash/bugulma/backend/internal/service/auth_service.go
Damir Mukimov 000eab4740
Major repository reorganization and missing backend endpoints implementation
Repository Structure:
- Move files from cluttered root directory into organized structure
- Create archive/ for archived data and scraper results
- Create bugulma/ for the complete application (frontend + backend)
- Create data/ for sample datasets and reference materials
- Create docs/ for comprehensive documentation structure
- Create scripts/ for utility scripts and API tools

Backend Implementation:
- Implement 3 missing backend endpoints identified in gap analysis:
  * GET /api/v1/organizations/{id}/matching/direct - Direct symbiosis matches
  * GET /api/v1/users/me/organizations - User organizations
  * POST /api/v1/proposals/{id}/status - Update proposal status
- Add complete proposal domain model, repository, and service layers
- Create database migration for proposals table
- Fix CLI server command registration issue

API Documentation:
- Add comprehensive proposals.md API documentation
- Update README.md with Users and Proposals API sections
- Document all request/response formats, error codes, and business rules

Code Quality:
- Follow existing Go backend architecture patterns
- Add proper error handling and validation
- Match frontend expected response schemas
- Maintain clean separation of concerns (handler -> service -> repository)
2025-11-25 06:01:16 +01:00

82 lines
2.0 KiB
Go

package service
import (
"context"
"errors"
"bugulma/backend/internal/domain"
"github.com/golang-jwt/jwt/v5"
"golang.org/x/crypto/bcrypt"
)
type AuthService struct {
userRepo domain.UserRepository
jwtService *JWTService
}
func NewAuthService(userRepo domain.UserRepository, jwtSecret string) *AuthService {
return &AuthService{
userRepo: userRepo,
jwtService: NewJWTService(jwtSecret),
}
}
type Claims struct {
UserID string `json:"user_id"`
Email string `json:"email"`
Name string `json:"name"`
Role string `json:"role"`
jwt.RegisteredClaims
}
func (s *AuthService) Login(ctx context.Context, email, password string) (string, *domain.User, error) {
user, err := s.userRepo.GetByEmail(ctx, email)
if err != nil {
return "", nil, errors.New("invalid credentials")
}
// Verify password
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
return "", nil, errors.New("invalid credentials")
}
// For now, use empty orgID - this would be determined by user's primary organization
// In a real implementation, this might involve checking user's organization memberships
token, err := s.jwtService.GenerateToken(user, "")
if err != nil {
return "", nil, err
}
return token, user, nil
}
func (s *AuthService) ValidateToken(ctx context.Context, tokenString string) (*domain.User, error) {
claims, err := s.jwtService.ValidateToken(tokenString)
if err != nil {
return nil, err
}
user, err := s.userRepo.GetByID(ctx, claims.UserID)
if err != nil {
return nil, err
}
return user, nil
}
// ValidateTokenWithClaims validates token and returns both user and claims
func (s *AuthService) ValidateTokenWithClaims(ctx context.Context, tokenString string) (*domain.User, *JWTClaims, error) {
claims, err := s.jwtService.ValidateToken(tokenString)
if err != nil {
return nil, nil, err
}
user, err := s.userRepo.GetByID(ctx, claims.UserID)
if err != nil {
return nil, nil, err
}
return user, claims, nil
}