mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-17 20:45:44 +00:00
re-implement password change
This commit is contained in:
@ -18,7 +18,7 @@ export enum ApiEndpoints {
|
|||||||
user_simple_login = 'email/generate/',
|
user_simple_login = 'email/generate/',
|
||||||
user_reset = 'auth/password/reset/', // TODO change
|
user_reset = 'auth/password/reset/', // TODO change
|
||||||
user_reset_set = 'auth/password/reset/confirm/', // TODO change
|
user_reset_set = 'auth/password/reset/confirm/', // TODO change
|
||||||
user_change_password = 'auth/password/change/', // TODO change
|
user_change_password = 'auth/v1/account/password/change',
|
||||||
user_sso = 'auth/v1/account/providers',
|
user_sso = 'auth/v1/account/providers',
|
||||||
user_login = 'auth/v1/auth/login',
|
user_login = 'auth/v1/auth/login',
|
||||||
user_login_mfa = 'auth/v1/auth/2fa/authenticate',
|
user_login_mfa = 'auth/v1/auth/2fa/authenticate',
|
||||||
|
@ -19,12 +19,14 @@ import { StylishText } from '../../components/items/StylishText';
|
|||||||
import { ProtectedRoute } from '../../components/nav/Layout';
|
import { ProtectedRoute } from '../../components/nav/Layout';
|
||||||
import { LanguageContext } from '../../contexts/LanguageContext';
|
import { LanguageContext } from '../../contexts/LanguageContext';
|
||||||
import { ApiEndpoints } from '../../enums/ApiEndpoints';
|
import { ApiEndpoints } from '../../enums/ApiEndpoints';
|
||||||
|
import { clearCsrfCookie } from '../../functions/auth';
|
||||||
import { apiUrl } from '../../states/ApiState';
|
import { apiUrl } from '../../states/ApiState';
|
||||||
import { useUserState } from '../../states/UserState';
|
import { useUserState } from '../../states/UserState';
|
||||||
|
|
||||||
export default function Set_Password() {
|
export default function Set_Password() {
|
||||||
const simpleForm = useForm({
|
const simpleForm = useForm({
|
||||||
initialValues: {
|
initialValues: {
|
||||||
|
current_password: '',
|
||||||
new_password1: '',
|
new_password1: '',
|
||||||
new_password2: ''
|
new_password2: ''
|
||||||
}
|
}
|
||||||
@ -37,6 +39,7 @@ export default function Set_Password() {
|
|||||||
let message: any =
|
let message: any =
|
||||||
values?.new_password2 ||
|
values?.new_password2 ||
|
||||||
values?.new_password1 ||
|
values?.new_password1 ||
|
||||||
|
values?.current_password ||
|
||||||
values?.error ||
|
values?.error ||
|
||||||
t`Password could not be changed`;
|
t`Password could not be changed`;
|
||||||
|
|
||||||
@ -55,27 +58,45 @@ export default function Set_Password() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleSet() {
|
function handleSet() {
|
||||||
|
const { clearUserState } = useUserState.getState();
|
||||||
|
|
||||||
|
// check if passwords match
|
||||||
|
if (simpleForm.values.new_password1 !== simpleForm.values.new_password2) {
|
||||||
|
passwordError({ new_password2: t`Passwords do not match` });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Set password with call to backend
|
// Set password with call to backend
|
||||||
api
|
api
|
||||||
.post(apiUrl(ApiEndpoints.user_change_password), {
|
.post(apiUrl(ApiEndpoints.user_change_password), {
|
||||||
new_password1: simpleForm.values.new_password1,
|
current_password: simpleForm.values.current_password,
|
||||||
new_password2: simpleForm.values.new_password2
|
new_password: simpleForm.values.new_password2
|
||||||
})
|
})
|
||||||
.then((val) => {
|
.then((val) => {
|
||||||
if (val.status === 200) {
|
passwordError(val.data);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
if (err.status === 401) {
|
||||||
notifications.show({
|
notifications.show({
|
||||||
title: t`Password Changed`,
|
title: t`Password Changed`,
|
||||||
message: t`The password was set successfully. You can now login with your new password`,
|
message: t`The password was set successfully. You can now login with your new password`,
|
||||||
color: 'green',
|
color: 'green',
|
||||||
autoClose: false
|
autoClose: false
|
||||||
});
|
});
|
||||||
|
clearUserState();
|
||||||
|
clearCsrfCookie();
|
||||||
navigate('/login');
|
navigate('/login');
|
||||||
} else {
|
} else {
|
||||||
passwordError(val.data);
|
// compile errors
|
||||||
|
const errors: { [key: string]: string[] } = {};
|
||||||
|
for (const val of err.response.data.errors) {
|
||||||
|
if (!errors[val.param]) {
|
||||||
|
errors[val.param] = [];
|
||||||
|
}
|
||||||
|
errors[val.param].push(val.message);
|
||||||
|
}
|
||||||
|
passwordError(errors);
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
passwordError(err.response.data);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,6 +118,13 @@ export default function Set_Password() {
|
|||||||
)}
|
)}
|
||||||
<Divider />
|
<Divider />
|
||||||
<Stack gap='xs'>
|
<Stack gap='xs'>
|
||||||
|
<PasswordInput
|
||||||
|
required
|
||||||
|
aria-label='password'
|
||||||
|
label={t`Current Password`}
|
||||||
|
description={t`Enter your current password`}
|
||||||
|
{...simpleForm.getInputProps('current_password')}
|
||||||
|
/>
|
||||||
<PasswordInput
|
<PasswordInput
|
||||||
required
|
required
|
||||||
aria-label='input-password-1'
|
aria-label='input-password-1'
|
||||||
|
Reference in New Issue
Block a user