Skip to content

Commit

Permalink
Update user menu
Browse files Browse the repository at this point in the history
  • Loading branch information
maidul98 committed Apr 15, 2023
1 parent 0044299 commit fe115a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
40 changes: 22 additions & 18 deletions cli/packages/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type params struct {
keyLength uint32
}

const ADD_USER = "Add a new account login"
const REPLACE_USER = "Override current logged in user"
const EXIT_USER_MENU = "Exit"

// loginCmd represents the login command
var loginCmd = &cobra.Command{
Use: "login",
Expand All @@ -48,26 +52,26 @@ var loginCmd = &cobra.Command{
util.HandleError(err)
}

addUser := false
if currentLoggedInUserDetails.UserCredentials.Email != "" {
addUser, err = addNewUserPrompt()
// addUser := false
// if currentLoggedInUserDetails.UserCredentials.Email != "" {
// addUser, err = addNewUserPrompt()
// if err != nil {
// util.HandleError(err)
// }
// }

// if !addUser {
if currentLoggedInUserDetails.IsUserLoggedIn && !currentLoggedInUserDetails.LoginExpired && len(currentLoggedInUserDetails.UserCredentials.PrivateKey) != 0 {
shouldOverride, err := userLoginMenu(currentLoggedInUserDetails.UserCredentials.Email)
if err != nil {
util.HandleError(err)
}
}

if !addUser {
if currentLoggedInUserDetails.IsUserLoggedIn && !currentLoggedInUserDetails.LoginExpired && len(currentLoggedInUserDetails.UserCredentials.PrivateKey) != 0 {
shouldOverride, err := shouldOverrideLoginPrompt(currentLoggedInUserDetails.UserCredentials.Email)
if err != nil {
util.HandleError(err)
}

if !shouldOverride {
return
}
if !shouldOverride {
return
}
}
// }

email, password, err := askForLoginCredentials()
if err != nil {
Expand Down Expand Up @@ -366,16 +370,16 @@ func addNewUserPrompt() (bool, error) {
return result == "Yes", err
}

func shouldOverrideLoginPrompt(currentLoggedInUserEmail string) (bool, error) {
func userLoginMenu(currentLoggedInUserEmail string) (bool, error) {
prompt := promptui.Select{
Label: fmt.Sprintf("There seems to be a user already logged in with the email: %s. Would you like to override that login? Select[Yes/No]", currentLoggedInUserEmail),
Items: []string{"No", "Yes"},
Label: fmt.Sprintf("Current logged in user email: %s", currentLoggedInUserEmail),
Items: []string{ADD_USER, REPLACE_USER, EXIT_USER_MENU},
}
_, result, err := prompt.Run()
if err != nil {
return false, err
}
return result == "Yes", err
return result != EXIT_USER_MENU, err
}

func generateFromPassword(password string, salt []byte, p *params) (hash []byte, err error) {
Expand Down
4 changes: 2 additions & 2 deletions cli/packages/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func WriteInitalConfig(userCredentials *models.UserCredentials) error {
return fmt.Errorf("writeInitalConfig: unable to write config file because [err=%s]", err)
}

//if empty
//if empty, initialize
if existingConfigFile.LoggedInUsersEmail == nil {
existingConfigFile.LoggedInUsersEmail = []string{}
}

//if profile exists
//if profiles exists
if len(existingConfigFile.LoggedInUsersEmail) > 0 {
ok := Contains(existingConfigFile.LoggedInUsersEmail, userCredentials.Email)
if !ok {
Expand Down

0 comments on commit fe115a7

Please sign in to comment.