32 lines
666 B
Go
32 lines
666 B
Go
package services
|
|
|
|
import (
|
|
"gitea.zjmud.com/phyer/rbac/models"
|
|
"gitea.zjmud.com/phyer/rbac/repositories"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type AuthService struct {
|
|
userRepo repositories.UserRepository
|
|
}
|
|
|
|
func NewAuthService(db *gorm.DB) *AuthService {
|
|
return &AuthService{
|
|
userRepo: repositories.NewUserRepository(db),
|
|
}
|
|
}
|
|
|
|
func (s *AuthService) Authenticate(username, password string) (*models.User, error) {
|
|
user, err := s.userRepo.FindByUsername(username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
hashedPassword, err := models.HashPassword(password)
|
|
if err != nil || string(hashedPassword) != user.Password {
|
|
return nil, ErrInvalidCredentials
|
|
}
|
|
|
|
return user, nil
|
|
}
|