diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-26 22:26:10 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-26 22:40:16 +0200 |
commit | 4d930c0c8cb585979798fac2bb254f991faa62fb (patch) | |
tree | 0c33e0e0f2a2f47b0f64843f7d9a3eb299abb260 /pkg/components/auth/controller.go | |
parent | d4e1ca3a48e74573df6965ceee217e119ff899ae (diff) | |
download | lens-4d930c0c8cb585979798fac2bb254f991faa62fb.tar.gz lens-4d930c0c8cb585979798fac2bb254f991faa62fb.tar.bz2 lens-4d930c0c8cb585979798fac2bb254f991faa62fb.zip |
feat: Add initial user setup
Diffstat (limited to 'pkg/components/auth/controller.go')
-rw-r--r-- | pkg/components/auth/controller.go | 44 |
1 files changed, 39 insertions, 5 deletions
diff --git a/pkg/components/auth/controller.go b/pkg/components/auth/controller.go index a81a1c0..2f30fb5 100644 --- a/pkg/components/auth/controller.go +++ b/pkg/components/auth/controller.go @@ -5,18 +5,26 @@ import ( "golang.org/x/crypto/bcrypt" + "git.sr.ht/~gabrielgio/img/pkg/components" + "git.sr.ht/~gabrielgio/img/pkg/components/user" "git.sr.ht/~gabrielgio/img/pkg/ext" ) type Controller struct { - repository Repository - key []byte + repository Repository + userRepository user.Repository + key []byte } -func NewController(repository Repository, key []byte) *Controller { +func NewController( + repository Repository, + userRepository user.Repository, + key []byte, +) *Controller { return &Controller{ - repository: repository, - key: key, + repository: repository, + userRepository: userRepository, + key: key, } } @@ -41,3 +49,29 @@ func (c *Controller) Login(ctx context.Context, username, password []byte) ([]by } return ext.WriteToken(token, c.key) } + +// InitialRegister register a initial user, it will validate if there is another +// user stored already. If so an error `InvlidaInput` will be returned +func (c *Controller) InitialRegister(ctx context.Context, username, password []byte, path []byte) error { + exist, err := c.userRepository.Any(ctx) + if err != nil { + return err + } + + if exist { + return components.InvlidaInput + } + + hash, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost) + if err != nil { + return err + } + + err = c.userRepository.Create(ctx, &user.CreateUser{ + Username: string(username), + Password: hash, + Path: string(path), + }) + + return err +} |