mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
feat: add ability to create an account via the webui (#223)
* feat: add ability to create an account via the webui without the need for autobrrctl * refactor redundant code block. * fix: early return and 0 value
This commit is contained in:
parent
982eddc269
commit
1a4f3cf55d
11 changed files with 337 additions and 109 deletions
|
@ -10,7 +10,9 @@ import (
|
|||
)
|
||||
|
||||
type Service interface {
|
||||
GetUserCount(ctx context.Context) (int, error)
|
||||
Login(ctx context.Context, username, password string) (*domain.User, error)
|
||||
CreateUser(ctx context.Context, username, password string) error
|
||||
}
|
||||
|
||||
type service struct {
|
||||
|
@ -23,9 +25,13 @@ func NewService(userSvc user.Service) Service {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *service) GetUserCount(ctx context.Context) (int, error) {
|
||||
return s.userSvc.GetUserCount(ctx)
|
||||
}
|
||||
|
||||
func (s *service) Login(ctx context.Context, username, password string) (*domain.User, error) {
|
||||
if username == "" || password == "" {
|
||||
return nil, errors.New("bad credentials")
|
||||
return nil, errors.New("empty credentials supplied")
|
||||
}
|
||||
|
||||
// find user
|
||||
|
@ -50,3 +56,33 @@ func (s *service) Login(ctx context.Context, username, password string) (*domain
|
|||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (s *service) CreateUser(ctx context.Context, username, password string) error {
|
||||
if username == "" || password == "" {
|
||||
return errors.New("empty credentials supplied")
|
||||
}
|
||||
|
||||
userCount, err := s.userSvc.GetUserCount(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if userCount > 0 {
|
||||
return errors.New("only 1 user account is supported at the moment")
|
||||
}
|
||||
|
||||
hashed, err := argon2id.CreateHash(password, argon2id.DefaultParams)
|
||||
if err != nil {
|
||||
return errors.New("failed to hash password")
|
||||
}
|
||||
|
||||
newUser := domain.User{
|
||||
Username: username,
|
||||
Password: hashed,
|
||||
}
|
||||
if err := s.userSvc.CreateUser(context.Background(), newUser); err != nil {
|
||||
return errors.New("failed to create new user")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue