Skip to content

Commit

Permalink
do not require email for signup with invite
Browse files Browse the repository at this point in the history
  • Loading branch information
byteplow committed May 13, 2024
1 parent e045667 commit 92a266f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
23 changes: 13 additions & 10 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,19 @@ def create

registration_method = SettingGetter.new(setting_name: 'RegistrationMethod', provider: current_provider).call

if registration_method == SiteSetting::REGISTRATION_METHODS[:invite] && !valid_invite_token && !admin_create
return render_error errors: Rails.configuration.custom_error_msgs[:invite_token_invalid]
if registration_method == SiteSetting::REGISTRATION_METHODS[:invite] && !admin_create
if create_user_params[:invite_token].blank?
return render_error errors: Rails.configuration.custom_error_msgs[:invite_token_invalid]
end

invite = Invitation.find_by(provider: current_provider, token: create_user_params[:invite_token])
if !invite.present?
return render_error errors: Rails.configuration.custom_error_msgs[:invite_token_invalid]
end

create_user_params[:email] = invite.email

invite.destroy
end

# TODO: Add proper error logging for non-verified token hcaptcha
Expand Down Expand Up @@ -172,14 +183,6 @@ def update_user_params
def change_password_params
params.require(:user).permit(:old_password, :new_password)
end

def valid_invite_token
return false if create_user_params[:invite_token].blank?

# Try to delete the invitation and return true if it succeeds
Invitation.destroy_by(email: create_user_params[:email].downcase, provider: current_provider,
token: create_user_params[:invite_token]).present?
end
end
end
end
2 changes: 1 addition & 1 deletion app/javascript/components/users/authentication/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default function Signup() {
</div>
<Card className="col-xl-5 col-lg-6 col-md-8 col-10 mx-auto p-4 border-0 card-shadow">
<Card.Title className="text-center pb-2"> { t('authentication.create_an_account') } </Card.Title>
<SignupForm />
<SignupForm registrationMethod={registrationMethodSettingAPI.data} />
<span className="text-center text-muted small"> { t('authentication.already_have_account') }
<Link to="/signin" className="text-link"> { t('authentication.sign_in') } </Link>
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import useCreateUser from '../../../../hooks/mutations/users/useCreateUser';
import useSignUpForm from '../../../../hooks/forms/users/authentication/useSignUpForm';
import HCaptcha from '../../../shared_components/utilities/HCaptcha';

export default function SignupForm() {
export default function SignupForm({registrationMethod}) {
const { t } = useTranslation();
const { fields, methods } = useSignUpForm();
const { fields, methods } = useSignUpForm(registrationMethod);
const createUserAPI = useCreateUser();
const captchaRef = useRef(null);

Expand All @@ -40,7 +40,9 @@ export default function SignupForm() {
return (
<Form methods={methods} onSubmit={handleSubmit}>
<FormControl field={fields.name} type="text" autoFocus />
{ registrationMethod !== 'invite' && (
<FormControl field={fields.email} type="email" />
)}
<FormControl field={fields.password} type="password" />
<FormControl field={fields.password_confirmation} type="password" />
<HCaptcha ref={captchaRef} />
Expand Down
22 changes: 13 additions & 9 deletions app/javascript/hooks/forms/users/authentication/useSignUpForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ import { useTranslation } from 'react-i18next';
import { useForm } from 'react-hook-form';
import { useCallback, useMemo } from 'react';

export function useSignUpFormValidation() {
return useMemo(() => (yup.object({
export function useSignUpFormValidation(registrationMethod) {
const spec = {
name: yup.string().required('forms.validations.full_name.required')
.min(2, 'forms.validations.full_name.min')
.max(255, 'forms.validations.full_name.max'),

email: yup.string().required('forms.validations.email.required').email('forms.validations.email.email')
.min(6, 'forms.validations.email.min')
.max(255, 'forms.validations.email.max'),

password: yup.string().max(255, 'forms.validations.password.max')
.matches(
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[`@%~!#£$\\^&*()\][+={}/|:;"'<>\-,.?_ ]).{8,}$/,
Expand All @@ -42,10 +38,18 @@ export function useSignUpFormValidation() {
.test('oneSymbol', 'forms.validations.password.symbol', (pwd) => pwd.match(/[`@%~!#£$\\^&*()\][+={}/|:;"'<>\-,.?_ ]/)),
password_confirmation: yup.string().required('forms.validations.password_confirmation.required')
.oneOf([yup.ref('password')], 'forms.validations.password_confirmation.match'),
})), []);
}

if (registrationMethod !== 'invite') {
spec.email = yup.string().required('forms.validations.email.required').email('forms.validations.email.email')
.min(6, 'forms.validations.email.min')
.max(255, 'forms.validations.email.max')
}

return useMemo(() => (yup.object(spec)), []);
}

export default function useSignUpForm({ defaultValues: _defaultValues, ..._config } = {}) {
export default function useSignUpForm(registrationMethod, { defaultValues: _defaultValues, ..._config } = {}) {
const { t, i18n } = useTranslation();

const fields = useMemo(() => ({
Expand Down Expand Up @@ -89,7 +93,7 @@ export default function useSignUpForm({ defaultValues: _defaultValues, ..._confi
},
}), [i18n.resolvedLanguage]);

const validationSchema = useSignUpFormValidation();
const validationSchema = useSignUpFormValidation(registrationMethod);

const config = useMemo(() => ({
...{
Expand Down

0 comments on commit 92a266f

Please sign in to comment.