Add support for providing user name and password when creating user

This commit is contained in:
Stefan Kostic
2022-02-09 22:53:46 +01:00
parent 3f4cc70d22
commit 80a4d4f20c
2 changed files with 14 additions and 5 deletions

View File

@@ -25,6 +25,8 @@ type CreateUserResponseBody struct {
func (controller *CreateUserController) CreateUser(c echo.Context) error { func (controller *CreateUserController) CreateUser(c echo.Context) error {
// optional parameters that we currently do not use // optional parameters that we currently do not use
type RequestBody struct { type RequestBody struct {
Login string `json:"login"`
Password string `json:"password"`
PartnerID string `json:"partnerid"` PartnerID string `json:"partnerid"`
AccountType string `json:"accounttype"` AccountType string `json:"accounttype"`
} }
@@ -33,7 +35,7 @@ func (controller *CreateUserController) CreateUser(c echo.Context) error {
if err := c.Bind(&body); err != nil { if err := c.Bind(&body); err != nil {
return err return err
} }
user, err := controller.svc.CreateUser(c.Request().Context()) user, err := controller.svc.CreateUser(c.Request().Context(), body.Login, body.Password)
//todo json response //todo json response
if err != nil { if err != nil {
return err return err

View File

@@ -10,13 +10,20 @@ import (
"github.com/uptrace/bun" "github.com/uptrace/bun"
) )
func (svc *LndhubService) CreateUser(ctx context.Context) (user *models.User, err error) { func (svc *LndhubService) CreateUser(ctx context.Context, login string, password string) (user *models.User, err error) {
user = &models.User{} user = &models.User{}
// generate user login/password (TODO: allow the user to choose a login/password?) // generate user login/password if not provided
user.Login = randStringBytes(20) user.Login = login
password := randStringBytes(20) if login == "" {
user.Login = randStringBytes(20)
}
if password == "" {
password = randStringBytes(20)
}
// we only store the hashed password but return the initial plain text password in the HTTP response // we only store the hashed password but return the initial plain text password in the HTTP response
hashedPassword := security.HashPassword(password) hashedPassword := security.HashPassword(password)
user.Password = hashedPassword user.Password = hashedPassword