Skip to content

Commit d50eb1d

Browse files
committed
feat(password): WIP
1 parent 309d839 commit d50eb1d

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

site/src/api/queries/users.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
UpdateUserProfileRequest,
99
User,
1010
UsersRequest,
11+
ValidateUserPasswordRequest,
1112
} from "api/typesGenerated";
1213
import {
1314
type MetadataState,
@@ -63,6 +64,15 @@ export const updatePassword = () => {
6364
};
6465
};
6566

67+
export const validatePassword = () => {
68+
return {
69+
mutationFn: ({
70+
...request
71+
}: ValidateUserPasswordRequest) =>
72+
API.validateUserPassword(request.password),
73+
};
74+
}
75+
6676
export const createUser = (queryClient: QueryClient) => {
6777
return {
6878
mutationFn: API.createUser,

site/src/api/typesGenerated.ts

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/src/pages/SetupPage/SetupPage.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { buildInfo } from "api/queries/buildInfo";
2+
import { validatePassword } from "api/queries/users";
23
import { createFirstUser } from "api/queries/users";
34
import { Loader } from "components/Loader/Loader";
45
import { useAuthContext } from "contexts/auth/AuthProvider";
@@ -25,10 +26,9 @@ export const SetupPage: FC = () => {
2526
const setupIsComplete = !isConfiguringTheFirstUser;
2627
const { metadata } = useEmbeddedMetadata();
2728
const buildInfoQuery = useQuery(buildInfo(metadata["build-info"]));
29+
const validatePasswordQuery = useQuery({...validatePassword(), enabled: true});
2830
const navigate = useNavigate();
2931

30-
const [isPasswordValid, setIsPasswordValid] = useState<boolean | null>(null);
31-
3232
useEffect(() => {
3333
if (!buildInfoQuery.data) {
3434
return;
@@ -54,7 +54,6 @@ export const SetupPage: FC = () => {
5454

5555
const validateUserPassword = async (password: string) => {
5656
const isValid = await validateUserPassword(password);
57-
setIsPasswordValid(isValid);
5857
};
5958

6059
const { debounced: debouncedValidateUserPassword } = useDebouncedFunction(validateUserPassword, 500);
@@ -65,7 +64,7 @@ export const SetupPage: FC = () => {
6564
<title>{pageTitle("Set up your account")}</title>
6665
</Helmet>
6766
<SetupPageView
68-
onPasswordChange={validateUserPassword}
67+
onPasswordChange={debouncedValidateUserPassword}
6968
isLoading={isSigningIn || createFirstUserMutation.isLoading}
7069
error={createFirstUserMutation.error}
7170
onSubmit={async (firstUser) => {

site/src/pages/SetupPage/SetupPageView.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,18 @@ const numberOfDevelopersOptions = [
8484
];
8585

8686
export interface SetupPageViewProps {
87+
onPasswordChange?: (password: string) => void;
8788
onSubmit: (firstUser: TypesGen.CreateFirstUserRequest) => void;
8889
error?: unknown;
8990
isLoading?: boolean;
9091
}
9192

9293
export const SetupPageView: FC<SetupPageViewProps> = ({
94+
onPasswordChange,
9395
onSubmit,
9496
error,
9597
isLoading,
9698
}) => {
97-
const [isPasswordValid, setIsPasswordValid] = useState<boolean | null>(null);
98-
99-
// Debounce function to validate password
100-
const validatePassword = debounce(async (password: string) => {
101-
const isValid = await validateUserPassword(password);
102-
setIsPasswordValid(isValid); // Update state based on response
103-
}, 500); // Adjust debounce time as needed
104-
10599
const form: FormikContextType<TypesGen.CreateFirstUserRequest> =
106100
useFormik<TypesGen.CreateFirstUserRequest>({
107101
initialValues: {
@@ -129,11 +123,7 @@ export const SetupPageView: FC<SetupPageViewProps> = ({
129123
);
130124

131125
useEffect(() => {
132-
if (form.values.password) {
133-
validatePassword(form.values.password); // Call the debounce function
134-
} else {
135-
setIsPasswordValid(null); // Reset validation state if password is empty
136-
}
126+
onPasswordChange?.(form.values.password);
137127
}, [form.values.password]); // Run effect when password changes
138128

139129
return (

0 commit comments

Comments
 (0)