Skip to content

Commit

Permalink
fix: reprompt for matching passwords on mismatch (#2758)
Browse files Browse the repository at this point in the history
- Previously we only re-prompted for the password confirmation.
  • Loading branch information
sreya committed Jul 1, 2022
1 parent 554d991 commit e5d5fa7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
43 changes: 23 additions & 20 deletions cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,29 @@ func login() *cobra.Command {
}

if password == "" {
password, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Enter a " + cliui.Styles.Field.Render("password") + ":",
Secret: true,
Validate: cliui.ValidateNotEmpty,
})
if err != nil {
return xerrors.Errorf("specify password prompt: %w", err)
}
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Confirm " + cliui.Styles.Field.Render("password") + ":",
Secret: true,
Validate: func(s string) error {
if s != password {
return xerrors.Errorf("Passwords do not match")
}
return nil
},
})
if err != nil {
return xerrors.Errorf("confirm password prompt: %w", err)
var matching bool

for !matching {
password, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Enter a " + cliui.Styles.Field.Render("password") + ":",
Secret: true,
Validate: cliui.ValidateNotEmpty,
})
if err != nil {
return xerrors.Errorf("specify password prompt: %w", err)
}
confirm, err := cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Confirm " + cliui.Styles.Field.Render("password") + ":",
Secret: true,
})
if err != nil {
return xerrors.Errorf("confirm password prompt: %w", err)
}

matching = confirm == password
if !matching {
_, _ = fmt.Fprintln(cmd.OutOrStdout(), cliui.Styles.Error.Render("Passwords do not match"))
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions cli/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/pty/ptytest"
)
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestLogin(t *testing.T) {
go func() {
defer close(doneChan)
err := root.ExecuteContext(ctx)
assert.ErrorIs(t, err, context.Canceled)
assert.NoError(t, err)
}()

matches := []string{
Expand All @@ -108,9 +109,15 @@ func TestLogin(t *testing.T) {
pty.ExpectMatch(match)
pty.WriteLine(value)
}

// Validate that we reprompt for matching passwords.
pty.ExpectMatch("Passwords do not match")
pty.ExpectMatch("password") // Re-prompt password.
cancel()
pty.ExpectMatch("Enter a " + cliui.Styles.Field.Render("password"))

pty.WriteLine("pass")
pty.ExpectMatch("Confirm")
pty.WriteLine("pass")
pty.ExpectMatch("Welcome to Coder")
<-doneChan
})

Expand Down

0 comments on commit e5d5fa7

Please sign in to comment.