Skip to content

Commit 7bded6a

Browse files
committed
feat(password): add details field to validate password endpoint
1 parent 175b4bf commit 7bded6a

File tree

9 files changed

+27
-28
lines changed

9 files changed

+27
-28
lines changed

coderd/users_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,8 +1109,6 @@ func TestUpdateUserPassword(t *testing.T) {
11091109
require.Equal(t, database.AuditActionWrite, auditor.AuditLogs()[numLogs-1].Action)
11101110
})
11111111

1112-
// FIXME: Re-enable the tests once real logic changed
1113-
// Currently there's no check in code to validate that users have to put the old password
11141112
t.Run("MemberCantUpdateOwnPasswordWithoutOldPassword", func(t *testing.T) {
11151113
t.Parallel()
11161114
client := coderdtest.New(t, nil)

site/src/api/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,11 +1322,11 @@ class ApiMethods {
13221322
await this.axios.put(`/api/v2/users/${userId}/password`, updatePassword);
13231323
};
13241324

1325-
validateUserPassword = async (password: string): Promise<boolean> => {
1325+
validateUserPassword = async (password: string): Promise<TypesGen.ValidateUserPasswordResponse> => {
13261326
const response = await this.axios.post("/api/v2/users/validate-password", {
13271327
password,
13281328
});
1329-
return response.data.valid;
1329+
return response.data;
13301330
};
13311331

13321332
getRoles = async (): Promise<Array<TypesGen.AssignableRoles>> => {

site/src/pages/CreateUserPage/CreateUserForm.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export interface CreateUserFormProps {
6464
onSubmit: (user: TypesGen.CreateUserRequestWithOrgs) => void;
6565
onCancel: () => void;
6666
onPasswordChange: (password: string) => void;
67-
passwordIsValid: boolean;
67+
passwordValidator: TypesGen.ValidateUserPasswordResponse;
6868
error?: unknown;
6969
isLoading: boolean;
7070
authMethods?: TypesGen.AuthMethods;
@@ -91,7 +91,7 @@ export const CreateUserForm: FC<
9191
onSubmit,
9292
onCancel,
9393
onPasswordChange,
94-
passwordIsValid,
94+
passwordValidator,
9595
error,
9696
isLoading,
9797
authMethods,
@@ -206,15 +206,15 @@ export const CreateUserForm: FC<
206206
(form.values.login_type !== "password" &&
207207
"No password required for this login type") ||
208208
(form.values.password !== "" &&
209-
!passwordIsValid &&
210-
"password is not strong enough."),
209+
!passwordValidator.valid &&
210+
passwordValidator.details),
211211
})}
212212
autoComplete="current-password"
213213
fullWidth
214214
id="password"
215215
data-testid="password-input"
216216
disabled={form.values.login_type !== "password"}
217-
error={!!(form.values.password !== "" && !passwordIsValid)}
217+
error={!!(form.values.password !== "" && !passwordValidator.valid)}
218218
label={Language.passwordLabel}
219219
type="password"
220220
/>

site/src/pages/CreateUserPage/CreateUserPage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ export const CreateUserPage: FC = () => {
2020
const authMethodsQuery = useQuery(authMethods());
2121
const validatePasswordMutation = useMutation(validatePassword());
2222

23-
const [passwordIsValid, setPasswordIsValid] = useState(false);
23+
const [passwordValidator, setPasswordValidator] = useState({valid: false, details: ""});
2424

2525
const validateUserPassword = async (password: string) => {
2626
validatePasswordMutation.mutate(password, {
2727
onSuccess: (data) => {
28-
setPasswordIsValid(data);
28+
setPasswordValidator({valid: data.valid, details: data.details})
2929
},
3030
});
3131
};
@@ -53,7 +53,7 @@ export const CreateUserPage: FC = () => {
5353
navigate("..", { relative: "path" });
5454
}}
5555
onPasswordChange={debouncedValidateUserPassword}
56-
passwordIsValid={passwordIsValid}
56+
passwordValidator={passwordValidator}
5757
isLoading={createUserMutation.isLoading}
5858
/>
5959
</Margins>

site/src/pages/SetupPage/SetupPage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const SetupPage: FC = () => {
2929
const buildInfoQuery = useQuery(buildInfo(metadata["build-info"]));
3030
const navigate = useNavigate();
3131

32-
const [passwordIsValid, setPasswordIsValid] = useState(false);
32+
const [passwordValidator, setPasswordValidator] = useState({valid: false, details: ""});
3333

3434
useEffect(() => {
3535
if (!buildInfoQuery.data) {
@@ -43,7 +43,7 @@ export const SetupPage: FC = () => {
4343
const validateUserPassword = async (password: string) => {
4444
validatePasswordMutation.mutate(password, {
4545
onSuccess: (data) => {
46-
setPasswordIsValid(data);
46+
setPasswordValidator({valid: data.valid, details: data.details})
4747
},
4848
});
4949
};
@@ -74,7 +74,7 @@ export const SetupPage: FC = () => {
7474
</Helmet>
7575
<SetupPageView
7676
onPasswordChange={debouncedValidateUserPassword}
77-
passwordIsValid={passwordIsValid}
77+
passwordValidator={passwordValidator}
7878
isLoading={isSigningIn || createFirstUserMutation.isLoading}
7979
error={createFirstUserMutation.error}
8080
onSubmit={async (firstUser) => {

site/src/pages/SetupPage/SetupPageView.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ const numberOfDevelopersOptions = [
8484
export interface SetupPageViewProps {
8585
onSubmit: (firstUser: TypesGen.CreateFirstUserRequest) => void;
8686
onPasswordChange?: (password: string) => void;
87-
passwordIsValid?: boolean;
87+
passwordValidator: TypesGen.ValidateUserPasswordResponse;
8888
error?: unknown;
8989
isLoading?: boolean;
9090
}
9191

9292
export const SetupPageView: FC<SetupPageViewProps> = ({
9393
onSubmit,
9494
onPasswordChange,
95-
passwordIsValid = true,
95+
passwordValidator,
9696
error,
9797
isLoading,
9898
}) => {
@@ -183,9 +183,9 @@ export const SetupPageView: FC<SetupPageViewProps> = ({
183183
id="password"
184184
label={Language.passwordLabel}
185185
type="password"
186-
error={!!(form.values.password !== "" && !passwordIsValid)}
186+
error={!!(form.values.password !== "" && !passwordValidator.valid)}
187187
helperText={
188-
!passwordIsValid ? "Password is not strong enough." : ""
188+
!passwordValidator.valid ? passwordValidator.details : ""
189189
}
190190
/>
191191
<label

site/src/pages/UserSettingsPage/SecurityPage/SecurityForm.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { FC } from "react";
88
import { useEffect } from "react";
99
import { getFormHelpers } from "utils/formUtils";
1010
import * as Yup from "yup";
11+
import type * as TypesGen from "api/typesGenerated";
1112

1213
interface SecurityFormValues {
1314
old_password: string;
@@ -42,7 +43,7 @@ export interface SecurityFormProps {
4243
disabled: boolean;
4344
isLoading: boolean;
4445
onPasswordChange: (password: string) => void;
45-
passwordIsValid: boolean;
46+
passwordValidator: TypesGen.ValidateUserPasswordResponse;
4647
onSubmit: (values: SecurityFormValues) => void;
4748
error?: unknown;
4849
}
@@ -51,7 +52,7 @@ export const SecurityForm: FC<SecurityFormProps> = ({
5152
disabled,
5253
isLoading,
5354
onPasswordChange,
54-
passwordIsValid,
55+
passwordValidator,
5556
onSubmit,
5657
error,
5758
}) => {
@@ -96,10 +97,10 @@ export const SecurityForm: FC<SecurityFormProps> = ({
9697
autoComplete="password"
9798
fullWidth
9899
label={Language.newPasswordLabel}
99-
error={!!(form.values.password !== "" && !passwordIsValid)}
100+
error={!!(form.values.password !== "" && !passwordValidator.valid)}
100101
helperText={
101-
form.values.password !== "" && !passwordIsValid
102-
? "Password is not strong enough."
102+
form.values.password !== "" && !passwordValidator.valid
103+
? passwordValidator.details
103104
: ""
104105
}
105106
type="password"

site/src/pages/UserSettingsPage/SecurityPage/SecurityPage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ export const SecurityPage: FC = () => {
2929
});
3030
const singleSignOnSection = useSingleSignOnSection();
3131

32-
const [passwordIsValid, setPasswordIsValid] = useState(false);
32+
const [passwordValidator, setPasswordValidator] = useState({valid: false, details: ""});
3333

3434
const validateUserPassword = async (password: string) => {
3535
validatePasswordMutation.mutate(password, {
3636
onSuccess: (data) => {
37-
setPasswordIsValid(data);
37+
setPasswordValidator({valid: data.valid, details: data.details})
3838
},
3939
});
4040
};
@@ -56,7 +56,7 @@ export const SecurityPage: FC = () => {
5656
error: updatePasswordMutation.error,
5757
isLoading: updatePasswordMutation.isLoading,
5858
onPasswordChange: debouncedValidateUserPassword,
59-
passwordIsValid: passwordIsValid,
59+
passwordValidator: passwordValidator,
6060
onSubmit: async (data) => {
6161
await updatePasswordMutation.mutateAsync({
6262
userId: me.id,

site/src/pages/UserSettingsPage/SecurityPage/SecurityPageView.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const defaultArgs: ComponentProps<typeof SecurityPageView> = {
1616
isLoading: false,
1717
onSubmit: action("onSubmit"),
1818
onPasswordChange: (password: string) => {},
19-
passwordIsValid: false,
19+
passwordValidator: {valid: false, details: ""},
2020
},
2121
},
2222
oidc: {

0 commit comments

Comments
 (0)