bigbuffet-rw/app/soapbox/features/verification/registration.tsx

189 lines
5.9 KiB
TypeScript
Raw Normal View History

2022-04-07 10:39:22 -07:00
import { AxiosError } from 'axios';
2022-03-21 11:09:01 -07:00
import * as React from 'react';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
2022-04-07 10:39:22 -07:00
import { useDispatch } from 'react-redux';
2022-03-21 11:09:01 -07:00
import { Redirect } from 'react-router-dom';
import { logIn, verifyCredentials } from 'soapbox/actions/auth';
import { fetchInstance } from 'soapbox/actions/instance';
2022-05-02 13:55:52 -07:00
import { startOnboarding } from 'soapbox/actions/onboarding';
2022-03-21 11:09:01 -07:00
import snackbar from 'soapbox/actions/snackbar';
import { createAccount, removeStoredVerification } from 'soapbox/actions/verification';
import { Button, Form, FormGroup, Input, Stack } from 'soapbox/components/ui';
import ValidationCheckmark from 'soapbox/components/validation-checkmark';
2022-04-07 10:39:22 -07:00
import { useAppSelector } from 'soapbox/hooks';
import { getRedirectUrl } from 'soapbox/utils/redirect';
2022-03-21 11:09:01 -07:00
const messages = defineMessages({
success: {
id: 'registrations.success',
defaultMessage: 'Welcome to {siteTitle}!',
},
usernameTaken: {
id: 'registrations.unprocessable_entity',
defaultMessage: 'This username has already been taken.',
},
error: {
id: 'registrations.error',
defaultMessage: 'Failed to register your account.',
},
minimumCharacters: {
id: 'registration.validation.minimum_characters',
defaultMessage: '8 characters',
},
capitalLetter: {
id: 'registration.validation.capital_letter',
defaultMessage: '1 capital letter',
},
lowercaseLetter: {
id: 'registration.validation.lowercase_letter',
defaultMessage: '1 lowercase letter',
},
});
2022-03-21 11:09:01 -07:00
const initialState = {
username: '',
password: '',
};
const hasUppercaseCharacter = (string: string) => {
for (let i = 0; i < string.length; i++) {
if (string.charAt(i) === string.charAt(i).toUpperCase() && string.charAt(i).match(/[a-z]/i)) {
return true;
}
}
return false;
};
const hasLowercaseCharacter = (string: string) => {
return string.toUpperCase() !== string;
};
2022-03-21 11:09:01 -07:00
const Registration = () => {
const dispatch = useDispatch();
const intl = useIntl();
2022-04-07 10:39:22 -07:00
const isLoading = useAppSelector((state) => state.verification.get('isLoading') as boolean);
const siteTitle = useAppSelector((state) => state.instance.title);
2022-03-21 11:09:01 -07:00
const [state, setState] = React.useState(initialState);
2022-04-07 10:39:22 -07:00
const [shouldRedirect, setShouldRedirect] = React.useState<boolean>(false);
2022-03-21 11:09:01 -07:00
const { username, password } = state;
2022-06-09 08:03:12 -07:00
const meetsLengthRequirements = React.useMemo(() => password.length >= 8, [password]);
const meetsCapitalLetterRequirements = React.useMemo(() => hasUppercaseCharacter(password), [password]);
const meetsLowercaseLetterRequirements = React.useMemo(() => hasLowercaseCharacter(password), [password]);
const hasValidPassword = meetsLengthRequirements && meetsCapitalLetterRequirements && meetsLowercaseLetterRequirements;
2022-06-09 08:03:12 -07:00
2022-03-21 11:09:01 -07:00
const handleSubmit = React.useCallback((event) => {
event.preventDefault();
dispatch(createAccount(username, password))
.then(() => dispatch(logIn(intl, username, password)))
2022-04-07 10:39:22 -07:00
.then(({ access_token }: any) => dispatch(verifyCredentials(access_token)))
2022-03-21 11:09:01 -07:00
.then(() => dispatch(fetchInstance()))
.then(() => {
setShouldRedirect(true);
removeStoredVerification();
2022-05-02 13:55:52 -07:00
dispatch(startOnboarding());
2022-03-21 11:09:01 -07:00
dispatch(
snackbar.success(
intl.formatMessage(messages.success, { siteTitle }),
2022-03-21 11:09:01 -07:00
),
);
})
2022-04-07 10:39:22 -07:00
.catch((error: AxiosError) => {
2022-03-21 11:09:01 -07:00
if (error?.response?.status === 422) {
dispatch(
snackbar.error(
intl.formatMessage(messages.usernameTaken),
2022-03-21 11:09:01 -07:00
),
);
} else {
dispatch(
snackbar.error(
intl.formatMessage(messages.error),
2022-03-21 11:09:01 -07:00
),
);
}
});
}, [username, password]);
const handleInputChange = React.useCallback((event) => {
event.persist();
setState((prevState) => ({ ...prevState, [event.target.name]: event.target.value }));
}, []);
if (shouldRedirect) {
const redirectUri = getRedirectUrl();
return <Redirect to={redirectUri} />;
2022-03-21 11:09:01 -07:00
}
return (
<div>
<div className='pb-4 sm:pb-10 mb-4 border-b border-gray-200 border-solid -mx-4 sm:-mx-10'>
<h1 className='text-center font-bold text-2xl'>
<FormattedMessage id='registration.header' defaultMessage='Register your account' />
2022-03-21 11:09:01 -07:00
</h1>
</div>
<div className='sm:pt-10 sm:w-2/3 md:w-1/2 mx-auto space-y-4'>
2022-04-04 08:54:00 -07:00
<Form onSubmit={handleSubmit}>
2022-03-21 11:09:01 -07:00
<FormGroup labelText='Your username'>
<Input
name='username'
type='text'
value={username}
onChange={handleInputChange}
required
icon={require('@tabler/icons/icons/at.svg')}
/>
</FormGroup>
<FormGroup labelText='Password'>
<Input
name='password'
type='password'
value={password}
onChange={handleInputChange}
required
data-testid='password-input'
2022-03-21 11:09:01 -07:00
/>
2022-06-09 08:03:12 -07:00
<Stack className='mt-2' space={1}>
<ValidationCheckmark
isValid={meetsLengthRequirements}
text={intl.formatMessage(messages.minimumCharacters)}
/>
<ValidationCheckmark
isValid={meetsCapitalLetterRequirements}
text={intl.formatMessage(messages.capitalLetter)}
/>
<ValidationCheckmark
isValid={meetsLowercaseLetterRequirements}
text={intl.formatMessage(messages.lowercaseLetter)}
/>
2022-06-09 08:03:12 -07:00
</Stack>
2022-03-21 11:09:01 -07:00
</FormGroup>
<div className='text-center'>
<Button
block
theme='primary'
type='submit'
disabled={isLoading || !hasValidPassword}
>
Register
</Button>
2022-03-21 11:09:01 -07:00
</div>
</Form>
</div>
</div>
);
};
export default Registration;