Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/develop/resources/scripts/components/dashboard/forms/UpdatePasswordForm.tsx
7454 views
1
import { Fragment } from 'react';
2
import { Actions, State, useStoreActions, useStoreState } from 'easy-peasy';
3
import { Form, Formik, FormikHelpers } from 'formik';
4
import Field from '@/components/elements/Field';
5
import * as Yup from 'yup';
6
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
7
import updateAccountPassword from '@/api/account/updateAccountPassword';
8
import { httpErrorToHuman } from '@/api/http';
9
import { ApplicationStore } from '@/state';
10
import tw from 'twin.macro';
11
import { Button } from '@/components/elements/button/index';
12
13
interface Values {
14
current: string;
15
password: string;
16
confirmPassword: string;
17
}
18
19
const schema = Yup.object().shape({
20
current: Yup.string().min(1).required('You must provide your current password.'),
21
password: Yup.string().min(8).required(),
22
confirmPassword: Yup.string().test(
23
'password',
24
'Password confirmation does not match the password you entered.',
25
function (value) {
26
return value === this.parent.password;
27
},
28
),
29
});
30
31
export default () => {
32
const user = useStoreState((state: State<ApplicationStore>) => state.user.data);
33
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
34
35
if (!user) {
36
return null;
37
}
38
39
const submit = (values: Values, { setSubmitting }: FormikHelpers<Values>) => {
40
clearFlashes('account:password');
41
updateAccountPassword({ ...values })
42
.then(() => {
43
// @ts-expect-error this is valid
44
window.location = '/auth/login';
45
})
46
.catch(error =>
47
addFlash({
48
key: 'account:password',
49
type: 'error',
50
title: 'Error',
51
message: httpErrorToHuman(error),
52
}),
53
)
54
.then(() => setSubmitting(false));
55
};
56
57
return (
58
<Fragment>
59
<Formik
60
onSubmit={submit}
61
validationSchema={schema}
62
initialValues={{ current: '', password: '', confirmPassword: '' }}
63
>
64
{({ isSubmitting, isValid }) => (
65
<Fragment>
66
<SpinnerOverlay size={'large'} visible={isSubmitting} />
67
<Form css={tw`m-0`}>
68
<Field
69
id={'current_password'}
70
type={'password'}
71
name={'current'}
72
label={'Current Password'}
73
/>
74
<div css={tw`mt-6`}>
75
<Field
76
id={'new_password'}
77
type={'password'}
78
name={'password'}
79
label={'New Password'}
80
description={
81
'Your new password should be at least 8 characters in length and unique to this website.'
82
}
83
/>
84
</div>
85
<div css={tw`mt-6`}>
86
<Field
87
id={'confirm_new_password'}
88
type={'password'}
89
name={'confirmPassword'}
90
label={'Confirm New Password'}
91
/>
92
</div>
93
<div css={tw`mt-6`}>
94
<Button disabled={isSubmitting || !isValid}>Update Password</Button>
95
</div>
96
</Form>
97
</Fragment>
98
)}
99
</Formik>
100
</Fragment>
101
);
102
};
103
104