2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 04:55:44 +00:00

re-implement registrations

This commit is contained in:
Matthias Mair
2025-01-09 00:52:14 +01:00
parent 31e25eb50b
commit 8ad07c49d5
3 changed files with 45 additions and 19 deletions

View File

@ -21,6 +21,7 @@ import { ApiEndpoints } from '../../enums/ApiEndpoints';
import { import {
doBasicLogin, doBasicLogin,
doSimpleLogin, doSimpleLogin,
ensureCsrf,
followRedirect followRedirect
} from '../../functions/auth'; } from '../../functions/auth';
import { showLoginNotification } from '../../functions/notifications'; import { showLoginNotification } from '../../functions/notifications';
@ -196,7 +197,12 @@ export function AuthenticationForm() {
export function RegistrationForm() { export function RegistrationForm() {
const registrationForm = useForm({ const registrationForm = useForm({
initialValues: { username: '', email: '', password1: '', password2: '' } initialValues: {
username: '',
email: '',
password: '',
password2: '' as string | undefined
}
}); });
const navigate = useNavigate(); const navigate = useNavigate();
const [auth_settings, registration_enabled, sso_registration] = const [auth_settings, registration_enabled, sso_registration] =
@ -207,14 +213,26 @@ export function RegistrationForm() {
]); ]);
const [isRegistering, setIsRegistering] = useState<boolean>(false); const [isRegistering, setIsRegistering] = useState<boolean>(false);
function handleRegistration() { async function handleRegistration() {
// check if passwords match
if (
registrationForm.values.password !== registrationForm.values.password2
) {
registrationForm.setFieldError('password2', t`Passwords do not match`);
return;
}
setIsRegistering(true); setIsRegistering(true);
// remove password2 from the request
const { password2, ...vals } = registrationForm.values;
await ensureCsrf();
api api
.post(apiUrl(ApiEndpoints.user_register), registrationForm.values, { .post(apiUrl(ApiEndpoints.user_register), vals, {
headers: { Authorization: '' } headers: { Authorization: '' }
}) })
.then((ret) => { .then((ret) => {
if (ret?.status === 204 || ret?.status === 201) { if (ret?.status === 200) {
setIsRegistering(false); setIsRegistering(false);
showLoginNotification({ showLoginNotification({
title: t`Registration successful`, title: t`Registration successful`,
@ -226,16 +244,23 @@ export function RegistrationForm() {
.catch((err) => { .catch((err) => {
if (err.response?.status === 400) { if (err.response?.status === 400) {
setIsRegistering(false); setIsRegistering(false);
for (const [key, value] of Object.entries(err.response.data)) {
registrationForm.setFieldError(key, value as string); // collect all errors per field
const errors: { [key: string]: string[] } = {};
for (const val of err.response.data.errors) {
if (!errors[val.param]) {
errors[val.param] = [];
} }
let err_msg = ''; errors[val.param].push(val.message);
if (err.response?.data?.non_field_errors) {
err_msg = err.response.data.non_field_errors;
} }
for (const key in errors) {
registrationForm.setFieldError(key, errors[key]);
}
showLoginNotification({ showLoginNotification({
title: t`Input error`, title: t`Input error`,
message: t`Check your input and try again. ` + err_msg, message: t`Check your input and try again. `,
success: false success: false
}); });
} }
@ -268,7 +293,7 @@ export function RegistrationForm() {
label={t`Password`} label={t`Password`}
aria-label='register-password' aria-label='register-password'
placeholder={t`Your password`} placeholder={t`Your password`}
{...registrationForm.getInputProps('password1')} {...registrationForm.getInputProps('password')}
/> />
<PasswordInput <PasswordInput
required required

View File

@ -23,7 +23,7 @@ export enum ApiEndpoints {
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',
user_logout = 'auth/v1/auth/session', user_logout = 'auth/v1/auth/session',
user_register = 'auth/registration/', // TODO change user_register = 'auth/v1/auth/signup',
user_mfa = 'auth/v1/account/authenticators', user_mfa = 'auth/v1/account/authenticators',
user_emails = 'auth/v1/account/email', user_emails = 'auth/v1/account/email',
login_provider_redirect = 'auth/v1/auth/provider/redirect', login_provider_redirect = 'auth/v1/auth/provider/redirect',

View File

@ -72,16 +72,10 @@ export const doBasicLogin = async (
} }
clearCsrfCookie(); clearCsrfCookie();
const cookie = getCsrfCookie(); await ensureCsrf();
const login_url = apiUrl(ApiEndpoints.user_login); const login_url = apiUrl(ApiEndpoints.user_login);
if (cookie == undefined) {
await api.get(apiUrl(ApiEndpoints.user_token)).catch(() => {
// his is to be expected
});
}
let loginDone = false; let loginDone = false;
let success = false; let success = false;
@ -169,6 +163,13 @@ export const doSimpleLogin = async (email: string) => {
return mail; return mail;
}; };
export async function ensureCsrf() {
const cookie = getCsrfCookie();
if (cookie == undefined) {
await api.get(apiUrl(ApiEndpoints.user_token)).catch(() => {});
}
}
export function handleReset( export function handleReset(
navigate: NavigateFunction, navigate: NavigateFunction,
values: { email: string } values: { email: string }