-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFinishSetup.tsx
More file actions
182 lines (170 loc) · 5.81 KB
/
FinishSetup.tsx
File metadata and controls
182 lines (170 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Breadcrumbs } from '@/components/Breadcrumbs';
import { Button } from '@/components/ui/button';
import { Form } from '@/components/ui/form/Form';
import { FormControl } from '@/components/ui/form/FormControl';
import { FormField } from '@/components/ui/form/FormField';
import { FormItem } from '@/components/ui/form/FormItem';
import { FormLabel } from '@/components/ui/form/FormLabel';
import { FormMessage } from '@/components/ui/form/FormMessage';
import { Input } from '@/components/ui/input';
import { defaultClusterUsername, defaultInstanceRouteUpOne } from '@/config/constants';
import { useInstanceClient } from '@/config/useInstanceClient';
import { authStore } from '@/features/auth/store/authStore';
import { useCloudAuth } from '@/hooks/useAuth';
import { AddUserFormSchema } from '@/integrations/api/instance/auth/addUserFormSchema';
import { useInstanceResetPasswordMutation } from '@/integrations/api/instance/auth/useInstanceResetPasswordMutation';
import { getOperationsUrlForCluster } from '@/lib/urls/getOperationsUrlForCluster';
import { zodResolver } from '@hookform/resolvers/zod';
import { useQuery } from '@tanstack/react-query';
import { Navigate, useNavigate, useParams, useRouter, useSearch } from '@tanstack/react-router';
import { ActivityIcon } from 'lucide-react';
import { useCallback, useEffect, useMemo } from 'react';
import { useForm } from 'react-hook-form';
import { toast } from 'sonner';
import { z } from 'zod';
import { getClusterInfoQueryOptions } from './queries/getClusterInfoQuery';
export function FinishSetup() {
const { user } = useCloudAuth();
const { clusterId }: { clusterId: string } = useParams({ strict: false });
const { data: cluster } = useQuery(
getClusterInfoQueryOptions(clusterId, true),
);
const navigate = useNavigate();
const operationsUrl = useMemo(() => getOperationsUrlForCluster(cluster), [cluster]);
const instanceClient = useInstanceClient({ operationsUrl });
const { redirect } = useSearch({ strict: false });
const router = useRouter();
const methods = useForm({
resolver: zodResolver(AddUserFormSchema),
defaultValues: {
confirmPassword: '',
password: '',
role: 'super_user',
username: user?.email ?? '',
},
});
const { setFocus, control, handleSubmit } = methods;
useEffect(() => {
setFocus('password');
}, [setFocus]);
const tempPassword = cluster?.instances?.find(i => i.tempPassword)?.tempPassword;
const { mutate: submitInstanceResetPassword, isPending } = useInstanceResetPasswordMutation();
const submitForm = useCallback(async (formData: z.infer<typeof AddUserFormSchema>) => {
if (!operationsUrl) {
toast.error('Cluster is not yet fully loaded, please wait a moment before trying to sign in.');
return;
}
submitInstanceResetPassword({
instanceClient,
clusterId,
newPassword: formData.password,
tempPassword,
initialUsername: defaultClusterUsername,
desiredUsername: formData.username,
}, {
onSuccess: async ({ message, user }) => {
toast.success(message);
authStore.setUserForEntity(cluster!, user);
void router.invalidate();
await navigate({ to: redirect?.startsWith('/') ? redirect : defaultInstanceRouteUpOne });
},
});
}, [
cluster,
clusterId,
instanceClient,
navigate,
operationsUrl,
redirect,
router,
submitInstanceResetPassword,
tempPassword,
]);
if (cluster && !cluster.resetPassword) {
return <Navigate to="../sign-in" replace={true} />;
}
return (
<>
<nav className="fixed top-20 w-full h-12 z-39 px-4 md:px-12 bg-grey-700 flex items-center">
<Breadcrumbs />
</nav>
<div className="items-center justify-center flex mt-32 py-4 min-h-[calc(100vh-(--spacing(32)))]">
<div className="text-white w-xs">
<h2 className="text-2xl font-light">Create Admin User</h2>
<p className="text-muted-foreground">
You are ready to create your first user in your new cluster. These credentials belong to you alone, and you
can create more users and roles once you create this first one.
</p>
<Form {...methods}>
<form
id="cluster-create-admin-form"
name="cluster-create-admin-form"
onSubmit={handleSubmit(submitForm)}
className="my-4"
>
<FormField
control={control}
name="username"
render={({ field }) => (
<FormItem className="my-4">
<FormLabel>Username</FormLabel>
<FormControl>
<Input
autoComplete="username"
type="text"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={control}
name="password"
render={({ field }) => (
<FormItem className="my-4">
<FormLabel>Password</FormLabel>
<FormControl>
<Input
type="password"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={control}
name="confirmPassword"
render={({ field }) => (
<FormItem className="my-4">
<FormLabel>Confirm Password</FormLabel>
<FormControl>
<Input
type="password"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button disabled={isPending} type="submit" variant="submit" className="w-full my-2 rounded-full">
Create Admin User
</Button>
</form>
</Form>
<p className="text-muted-foreground flex gap-2 align-middle">
<ActivityIcon size={36} className="flex-none" />
<span>
These credentials will be used to sign into your cluster directly, providing you a secure connection from
your browser to your cluster.
</span>
</p>
</div>
</div>
</>
);
}