43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package adminauth
|
|
|
|
import "errors"
|
|
|
|
var (
|
|
ErrDependencyUnavailable = errors.New("dependency unavailable")
|
|
ErrInvalidCredential = errors.New("invalid credential")
|
|
ErrCaptchaInvalid = errors.New("captcha invalid")
|
|
ErrAdminDisabled = errors.New("admin disabled")
|
|
)
|
|
|
|
type Service struct {
|
|
repo *Repository
|
|
}
|
|
|
|
func NewService(repo *Repository) *Service {
|
|
return &Service{repo: repo}
|
|
}
|
|
|
|
func (s *Service) Captcha() (*CaptchaDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.Captcha()
|
|
}
|
|
|
|
func (s *Service) Login(username string, password string, captchaID string, captchaCode string) (LoginResult, error) {
|
|
if s.repo == nil {
|
|
return LoginResult{}, ErrDependencyUnavailable
|
|
}
|
|
if username == "" || password == "" || captchaID == "" || captchaCode == "" {
|
|
return LoginResult{}, ErrInvalidCredential
|
|
}
|
|
return s.repo.Login(username, password, captchaID, captchaCode)
|
|
}
|
|
|
|
func (s *Service) Me(adminID uint64) (*AdminDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.FindByID(adminID)
|
|
}
|