2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-17 04:25:42 +00:00

fix password reset flow

This commit is contained in:
Matthias Mair
2025-01-19 23:44:43 +01:00
parent 2e8b1e7fd1
commit 47ece105e5
3 changed files with 36 additions and 30 deletions

View File

@ -16,8 +16,8 @@ export enum ApiEndpoints {
user_token = 'user/token/', user_token = 'user/token/',
user_tokens = 'user/tokens/', user_tokens = 'user/tokens/',
user_simple_login = 'email/generate/', user_simple_login = 'email/generate/',
user_reset = 'auth/password/reset/', // TODO change user_reset = 'auth/v1/auth/password/request',
user_reset_set = 'auth/password/reset/confirm/', // TODO change user_reset_set = 'auth/v1/auth/password/reset',
auth_pwd_change = 'auth/v1/account/password/change', auth_pwd_change = 'auth/v1/account/password/change',
auth_login = 'auth/v1/auth/login', auth_login = 'auth/v1/auth/login',
auth_login_2fa = 'auth/v1/auth/2fa/authenticate', auth_login_2fa = 'auth/v1/auth/2fa/authenticate',

View File

@ -172,10 +172,16 @@ export function handleReset(
navigate: NavigateFunction, navigate: NavigateFunction,
values: { email: string } values: { email: string }
) { ) {
ensureCsrf();
api api
.post(apiUrl(ApiEndpoints.user_reset), values, { .post(
apiUrl(ApiEndpoints.user_reset),
values
/*{
headers: { Authorization: '' } headers: { Authorization: '' }
}) }
*/
)
.then((val) => { .then((val) => {
if (val.status === 200) { if (val.status === 200) {
notifications.show({ notifications.show({

View File

@ -22,32 +22,41 @@ export default function ResetPassword() {
const [searchParams] = useSearchParams(); const [searchParams] = useSearchParams();
const navigate = useNavigate(); const navigate = useNavigate();
const token = searchParams.get('token'); const key = searchParams.get('key');
const uid = searchParams.get('uid');
function invalidToken() { function invalidKey() {
notifications.show({ notifications.show({
title: t`Token invalid`, title: t`Key invalid`,
message: t`You need to provide a valid token to set a new password. Check your inbox for a reset link.`, message: t`You need to provide a valid key to set a new password. Check your inbox for a reset link.`,
color: 'red' color: 'red'
}); });
navigate('/login'); navigate('/login');
} }
function success() {
notifications.show({
title: t`Password set`,
message: t`The password was set successfully. You can now login with your new password`,
color: 'green',
autoClose: false
});
navigate('/login');
}
function passwordError(values: any) { function passwordError(values: any) {
notifications.show({ notifications.show({
title: t`Reset failed`, title: t`Reset failed`,
message: values?.new_password2 || values?.new_password1 || values?.token, message: values?.errors.map((e: any) => e.message).join('\n'),
color: 'red' color: 'red'
}); });
} }
useEffect(() => { useEffect(() => {
// make sure we have a token // make sure we have a key
if (!token || !uid) { if (!key) {
invalidToken(); invalidKey();
} }
}, [token]); }, [key]);
function handleSet() { function handleSet() {
// Set password with call to backend // Set password with call to backend
@ -55,32 +64,23 @@ export default function ResetPassword() {
.post( .post(
apiUrl(ApiEndpoints.user_reset_set), apiUrl(ApiEndpoints.user_reset_set),
{ {
uid: uid, key: key,
token: token, password: simpleForm.values.password
new_password1: simpleForm.values.password,
new_password2: simpleForm.values.password
}, },
{ headers: { Authorization: '' } } { headers: { Authorization: '' } }
) )
.then((val) => { .then((val) => {
if (val.status === 200) { if (val.status === 200) {
notifications.show({ success();
title: t`Password set`,
message: t`The password was set successfully. You can now login with your new password`,
color: 'green',
autoClose: false
});
navigate('/login');
} else { } else {
passwordError(val.data); passwordError(val.data);
} }
}) })
.catch((err) => { .catch((err) => {
if ( if (err.response?.status === 400) {
err.response?.status === 400 && passwordError(err.response.data);
err.response?.data?.token == 'Invalid value' } else if (err.response?.status === 401) {
) { success();
invalidToken();
} else { } else {
passwordError(err.response.data); passwordError(err.response.data);
} }