Skip to content

Commit

Permalink
Add user: Make user add idempotent
Browse files Browse the repository at this point in the history
Closes #201
Signed-off-by: Lukas Kämmerling <[email protected]>
  • Loading branch information
LKaemmerling committed May 2, 2022
1 parent 9154926 commit c649257
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions pkg/cli/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"errors"
"fmt"

"github.com/usefathom/fathom/pkg/models"

log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/usefathom/fathom/pkg/datastore"
"github.com/usefathom/fathom/pkg/models"
)

var userCmd = cli.Command{
Expand Down Expand Up @@ -58,20 +59,30 @@ func userAdd(c *cli.Context) error {
return errors.New("Invalid arguments: missing password")
}

user := models.NewUser(email, password)
_, err := app.database.GetUserByEmail(email)
if err != nil {
if err == datastore.ErrNoResults {
user := models.NewUser(email, password)

// set password manually if --skip-bcrypt was given
// this is used to supply an already encrypted password string
if c.Bool("skip-bcrypt") {
user.Password = password
}
// set password manually if --skip-bcrypt was given
// this is used to supply an already encrypted password string
if c.Bool("skip-bcrypt") {
user.Password = password
}

if err := app.database.SaveUser(&user); err != nil {
return fmt.Errorf("Error creating user: %s", err)
}
if err := app.database.SaveUser(&user); err != nil {
return fmt.Errorf("Error creating user: %s", err)
}

log.Infof("Created user %s", user.Email)
return nil
}

log.Infof("Created user %s", user.Email)
return err
}
log.Infof("A user with this email %s already exists", email)
return nil

}

func userDelete(c *cli.Context) error {
Expand Down

0 comments on commit c649257

Please sign in to comment.