Skip to content

Commit f6ee0e1

Browse files
committed
feat(password): apply backend logic to all password set fields
1 parent d425661 commit f6ee0e1

File tree

8 files changed

+44
-26
lines changed

8 files changed

+44
-26
lines changed

site/src/api/api.ts

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

1325-
validateUserPassword = async (
1326-
password: string,
1327-
): Promise<boolean> => {
1328-
const response = await this.axios.post("/api/v2/users/validate-password", { password });
1325+
validateUserPassword = async (password: string): Promise<boolean> => {
1326+
const response = await this.axios.post("/api/v2/users/validate-password", {
1327+
password,
1328+
});
13291329
return response.data.valid;
13301330
};
13311331

site/src/api/queries/users.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ export const updatePassword = () => {
6767
export const validatePassword = () => {
6868
return {
6969
mutationFn: API.validateUserPassword,
70-
}
71-
}
70+
};
71+
};
7272

7373
export const createUser = (queryClient: QueryClient) => {
7474
return {

site/src/pages/CreateUserPage/CreateUserForm.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,15 @@ const validationSchema = Yup.object({
8787

8888
export const CreateUserForm: FC<
8989
React.PropsWithChildren<CreateUserFormProps>
90-
> = ({ onSubmit, onCancel, onPasswordChange, passwordIsValid, error, isLoading, authMethods }) => {
90+
> = ({
91+
onSubmit,
92+
onCancel,
93+
onPasswordChange,
94+
passwordIsValid,
95+
error,
96+
isLoading,
97+
authMethods,
98+
}) => {
9199
const form: FormikContextType<TypesGen.CreateUserRequestWithOrgs> =
92100
useFormik<TypesGen.CreateUserRequestWithOrgs>({
93101
initialValues: {
@@ -110,7 +118,6 @@ export const CreateUserForm: FC<
110118
onPasswordChange?.(form.values.password);
111119
}, [form.values.password, onPasswordChange]); // Run effect when password changes
112120

113-
114121
const methods = [
115122
authMethods?.password.enabled && "password",
116123
authMethods?.oidc.enabled && "oidc",
@@ -197,9 +204,8 @@ export const CreateUserForm: FC<
197204
{...getFieldHelpers("password", {
198205
helperText:
199206
(form.values.login_type !== "password" &&
200-
"No password required for this login type") ||
201-
(!passwordIsValid && "password is not strong.")
202-
207+
"No password required for this login type") ||
208+
(!passwordIsValid && "password is not strong."),
203209
})}
204210
autoComplete="current-password"
205211
fullWidth

site/src/pages/CreateUserPage/CreateUserPage.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { authMethods, createUser, validatePassword } from "api/queries/users";
22
import { displaySuccess } from "components/GlobalSnackbar/utils";
33
import { Margins } from "components/Margins/Margins";
4+
import { useDebouncedFunction } from "hooks/debounce";
45
import { type FC, useState } from "react";
56
import { Helmet } from "react-helmet-async";
67
import { useMutation, useQuery, useQueryClient } from "react-query";
78
import { useNavigate } from "react-router-dom";
89
import { pageTitle } from "utils/page";
910
import { CreateUserForm } from "./CreateUserForm";
10-
import { useDebouncedFunction } from "hooks/debounce";
1111

1212
export const Language = {
1313
unknownError: "Oops, an unknown error occurred.",
@@ -27,10 +27,13 @@ export const CreateUserPage: FC = () => {
2727
onSuccess: (data) => {
2828
setPasswordIsValid(data);
2929
},
30-
})
30+
});
3131
};
3232

33-
const { debounced: debouncedValidateUserPassword } = useDebouncedFunction(validateUserPassword, 500);
33+
const { debounced: debouncedValidateUserPassword } = useDebouncedFunction(
34+
validateUserPassword,
35+
500,
36+
);
3437

3538
return (
3639
<Margins>

site/src/pages/SetupPage/SetupPage.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { validatePassword } from "api/queries/users";
33
import { createFirstUser } from "api/queries/users";
44
import { Loader } from "components/Loader/Loader";
55
import { useAuthContext } from "contexts/auth/AuthProvider";
6+
import { useDebouncedFunction } from "hooks/debounce";
67
import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata";
78
import { type FC, useEffect, useState } from "react";
89
import { Helmet } from "react-helmet-async";
@@ -11,7 +12,6 @@ import { Navigate, useNavigate } from "react-router-dom";
1112
import { pageTitle } from "utils/page";
1213
import { sendDeploymentEvent } from "utils/telemetry";
1314
import { SetupPageView } from "./SetupPageView";
14-
import { useDebouncedFunction } from "hooks/debounce";
1515

1616
export const SetupPage: FC = () => {
1717
const {
@@ -59,10 +59,13 @@ export const SetupPage: FC = () => {
5959
onSuccess: (data) => {
6060
setPasswordIsValid(data);
6161
},
62-
})
62+
});
6363
};
6464

65-
const { debounced: debouncedValidateUserPassword } = useDebouncedFunction(validateUserPassword, 500);
65+
const { debounced: debouncedValidateUserPassword } = useDebouncedFunction(
66+
validateUserPassword,
67+
500,
68+
);
6669

6770
return (
6871
<>

site/src/pages/SetupPage/SetupPageView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { SignInLayout } from "components/SignInLayout/SignInLayout";
1414
import { Stack } from "components/Stack/Stack";
1515
import { type FormikContextType, useFormik } from "formik";
1616
import type { FC } from "react";
17+
import { useEffect } from "react";
1718
import { docs } from "utils/docs";
1819
import {
1920
getFormHelpers,
@@ -22,7 +23,6 @@ import {
2223
} from "utils/formUtils";
2324
import * as Yup from "yup";
2425
import { countries } from "./countries";
25-
import { useEffect } from "react";
2626

2727
export const Language = {
2828
emailLabel: "Email",

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { ErrorAlert } from "components/Alert/ErrorAlert";
55
import { Form, FormFields } from "components/Form/Form";
66
import { type FormikContextType, useFormik } from "formik";
77
import type { FC } from "react";
8+
import { useEffect } from "react";
89
import { getFormHelpers } from "utils/formUtils";
910
import * as Yup from "yup";
10-
import { useEffect } from "react";
1111

1212
interface SecurityFormValues {
1313
old_password: string;
@@ -30,8 +30,7 @@ export const Language = {
3030

3131
const validationSchema = Yup.object({
3232
old_password: Yup.string().trim().required(Language.oldPasswordRequired),
33-
password: Yup.string()
34-
.trim().required(Language.newPasswordRequired),
33+
password: Yup.string().trim().required(Language.newPasswordRequired),
3534
confirm_password: Yup.string()
3635
.trim()
3736
.test("passwords-match", Language.confirmPasswordMatch, function (value) {
@@ -43,7 +42,7 @@ export interface SecurityFormProps {
4342
disabled: boolean;
4443
isLoading: boolean;
4544
onPasswordChange: (password: string) => void;
46-
passwordIsValid: boolean,
45+
passwordIsValid: boolean;
4746
onSubmit: (values: SecurityFormValues) => void;
4847
error?: unknown;
4948
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { API } from "api/api";
2-
import { authMethods, updatePassword, validatePassword } from "api/queries/users";
2+
import {
3+
authMethods,
4+
updatePassword,
5+
validatePassword,
6+
} from "api/queries/users";
37
import { displaySuccess } from "components/GlobalSnackbar/utils";
48
import { Loader } from "components/Loader/Loader";
59
import { Stack } from "components/Stack/Stack";
610
import { useAuthenticated } from "contexts/auth/RequireAuth";
11+
import { useDebouncedFunction } from "hooks/debounce";
712
import { type ComponentProps, type FC, useState } from "react";
813
import { useMutation, useQuery } from "react-query";
914
import { Section } from "../Section";
@@ -12,7 +17,6 @@ import {
1217
SingleSignOnSection,
1318
useSingleSignOnSection,
1419
} from "./SingleSignOnSection";
15-
import { useDebouncedFunction } from "hooks/debounce";
1620

1721
export const SecurityPage: FC = () => {
1822
const { user: me } = useAuthenticated();
@@ -32,10 +36,13 @@ export const SecurityPage: FC = () => {
3236
onSuccess: (data) => {
3337
setPasswordIsValid(data);
3438
},
35-
})
39+
});
3640
};
3741

38-
const { debounced: debouncedValidateUserPassword } = useDebouncedFunction(validateUserPassword, 500);
42+
const { debounced: debouncedValidateUserPassword } = useDebouncedFunction(
43+
validateUserPassword,
44+
500,
45+
);
3946

4047
if (!authMethodsQuery.data || !userLoginType) {
4148
return <Loader />;

0 commit comments

Comments
 (0)