Skip to content

Commit e5d5fa7

Browse files
authored
fix: reprompt for matching passwords on mismatch (coder#2758)
- Previously we only re-prompted for the password confirmation.
1 parent 554d991 commit e5d5fa7

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

cli/login.go

+23-20
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,29 @@ func login() *cobra.Command {
131131
}
132132

133133
if password == "" {
134-
password, err = cliui.Prompt(cmd, cliui.PromptOptions{
135-
Text: "Enter a " + cliui.Styles.Field.Render("password") + ":",
136-
Secret: true,
137-
Validate: cliui.ValidateNotEmpty,
138-
})
139-
if err != nil {
140-
return xerrors.Errorf("specify password prompt: %w", err)
141-
}
142-
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
143-
Text: "Confirm " + cliui.Styles.Field.Render("password") + ":",
144-
Secret: true,
145-
Validate: func(s string) error {
146-
if s != password {
147-
return xerrors.Errorf("Passwords do not match")
148-
}
149-
return nil
150-
},
151-
})
152-
if err != nil {
153-
return xerrors.Errorf("confirm password prompt: %w", err)
134+
var matching bool
135+
136+
for !matching {
137+
password, err = cliui.Prompt(cmd, cliui.PromptOptions{
138+
Text: "Enter a " + cliui.Styles.Field.Render("password") + ":",
139+
Secret: true,
140+
Validate: cliui.ValidateNotEmpty,
141+
})
142+
if err != nil {
143+
return xerrors.Errorf("specify password prompt: %w", err)
144+
}
145+
confirm, err := cliui.Prompt(cmd, cliui.PromptOptions{
146+
Text: "Confirm " + cliui.Styles.Field.Render("password") + ":",
147+
Secret: true,
148+
})
149+
if err != nil {
150+
return xerrors.Errorf("confirm password prompt: %w", err)
151+
}
152+
153+
matching = confirm == password
154+
if !matching {
155+
_, _ = fmt.Fprintln(cmd.OutOrStdout(), cliui.Styles.Error.Render("Passwords do not match"))
156+
}
154157
}
155158
}
156159

cli/login_test.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/stretchr/testify/require"
99

1010
"github.com/coder/coder/cli/clitest"
11+
"github.com/coder/coder/cli/cliui"
1112
"github.com/coder/coder/coderd/coderdtest"
1213
"github.com/coder/coder/pty/ptytest"
1314
)
@@ -92,7 +93,7 @@ func TestLogin(t *testing.T) {
9293
go func() {
9394
defer close(doneChan)
9495
err := root.ExecuteContext(ctx)
95-
assert.ErrorIs(t, err, context.Canceled)
96+
assert.NoError(t, err)
9697
}()
9798

9899
matches := []string{
@@ -108,9 +109,15 @@ func TestLogin(t *testing.T) {
108109
pty.ExpectMatch(match)
109110
pty.WriteLine(value)
110111
}
112+
113+
// Validate that we reprompt for matching passwords.
111114
pty.ExpectMatch("Passwords do not match")
112-
pty.ExpectMatch("password") // Re-prompt password.
113-
cancel()
115+
pty.ExpectMatch("Enter a " + cliui.Styles.Field.Render("password"))
116+
117+
pty.WriteLine("pass")
118+
pty.ExpectMatch("Confirm")
119+
pty.WriteLine("pass")
120+
pty.ExpectMatch("Welcome to Coder")
114121
<-doneChan
115122
})
116123

0 commit comments

Comments
 (0)