package service import ( "gitea.qpismont.fr/qpismont/trepa/internal/accounts/domain" "gitea.qpismont.fr/qpismont/trepa/internal/core" ) type Service struct { repository domain.AccountRepository } func NewService(repository domain.AccountRepository) domain.AccountService { return &Service{repository: repository} } func (s *Service) GetAccount(id int) (*domain.Account, *core.HTTPError) { account, err := s.repository.FetchOneById(id) if err != nil { return nil, core.NewInternalServerError(err) } if account == nil { return nil, domain.ErrAccountNotFound } return account, nil } func (s *Service) Login(login domain.AccountLogin) (*domain.AccountWithToken, *core.HTTPError) { account, err := s.repository.FetchOneByUsername(login.Username) if err != nil { return nil, core.NewInternalServerError(err) } if account == nil { return nil, domain.ErrAccountNotFound } ok, err := core.ComparePassword(login.Password, account.Password) if err != nil { return nil, core.NewInternalServerError(err) } if !ok { return nil, domain.ErrBadPassword } claims := core.JWTClaims{ AccountId: account.Id, RoleId: account.RoleId, } token, err := core.SignJWT(claims) if err != nil { return nil, core.NewInternalServerError(err) } return &domain.AccountWithToken{ Account: account, Token: token, }, nil }