Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Commit

Permalink
feat(account): add register call and create matching command
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineDao committed Oct 24, 2019
1 parent 1939a37 commit 5624d1c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
83 changes: 82 additions & 1 deletion cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,41 @@ import (
"fmt"
"os"

gospeckle "github.com/speckleworks/gospeckle/pkg"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var userName string
var userSurname string
var company string
var password string

func init() {
rootCmd.AddCommand(loginCmd)
rootCmd.AddCommand(accountCmd)

accountCmd.AddCommand(loginCmd)
accountCmd.AddCommand(registerCmd)

loginCmd.Flags().StringVarP(&password, "password", "p", "", "The password for the user in the specified or current config")
loginCmd.MarkFlagRequired("password")

registerCmd.Flags().StringVar(&userName, "name", "", "name of the user")
registerCmd.Flags().StringVar(&userSurname, "surname", "", "surname of the user")
registerCmd.Flags().StringVar(&email, "email", "", "user's email")
registerCmd.Flags().StringVar(&company, "company", "", "company the user works for")
registerCmd.Flags().StringVar(&password, "password", "", "new user's password")
registerCmd.MarkFlagRequired("email")
registerCmd.MarkFlagRequired("password")
}

// loginCmd represents the login command
var accountCmd = &cobra.Command{
Use: "account",
Short: "commands to manage accounts specifically",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

// loginCmd represents the login command
Expand Down Expand Up @@ -43,3 +67,60 @@ var loginCmd = &cobra.Command{
fmt.Printf("Succesfully logged into %v with user email %v", currentConfig.Server.Host, currentConfig.User.Email)
},
}

// loginCmd represents the login command
var registerCmd = &cobra.Command{
Use: "register",
Short: "Register a new user to the current config's server and set as new context",
Run: func(cmd *cobra.Command, args []string) {
newUser := gospeckle.AccountRegisterRequest{
Name: userName,
Surname: userSurname,
Email: email,
Company: company,
Password: password,
}

err := speckleClient.Account.Register(ctx, newUser)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

err = speckleClient.Login(ctx, email, password, true)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

config, err := getConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

newConfigUser := ConfigUser{
Name: userName,
Email: email,
Token: speckleClient.Token,
}

serverName := currentConfig.Server.Name

newConfigContext := ConfigContext{
Name: serverName + "-" + newConfigUser.Name,
User: newConfigUser.Name,
Server: serverName,
}

config.Users = append(config.Users, newConfigUser)
config.Contexts = append(config.Contexts, newConfigContext)

viper.Set("users", config.Users)
viper.Set("contexts", config.Contexts)
viper.Set("current-context", newConfigContext.Name)

viper.WriteConfig()
fmt.Printf("Succesfully registered %v to server %v", newConfigUser.Name, currentConfig.Server.Host)
},
}
21 changes: 21 additions & 0 deletions pkg/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ type AccountUpdateRequest struct {
Company string `json:"company,omitempty"`
}

// AccountRegisterRequest is the request payload used to create a new account
type AccountRegisterRequest struct {
Name string `json:"name,omitempty"`
Surname string `json:"surname,omitempty"`
Email string `json:"email,omitempty"`
Company string `json:"company,omitempty"`
Password string `json:"password"`
}

// AccountRoleUpdateRequest is the request payload used to update the role of an account
type AccountRoleUpdateRequest struct {
Role string `json:"role"`
Expand Down Expand Up @@ -77,6 +86,18 @@ func (s *AccountService) Get(ctx context.Context, id string) (Account, *http.Res
return *account, resp, nil
}

// Register is the function used to login an existing user with an email and a password.
func (s *AccountService) Register(ctx context.Context, account AccountRegisterRequest) error {
req, err := s.client.NewRequest(ctx, http.MethodPost, "accounts/register", account)
if err != nil {
return err
}

_, _, err = s.client.Do(ctx, req, nil, false)

return err
}

// AdminGet retrieves all user accounts on the server
func (s *AccountService) AdminGet(ctx context.Context) ([]Account, *http.Response, error) {
account := new([]Account)
Expand Down

0 comments on commit 5624d1c

Please sign in to comment.