Skip to content

Commit

Permalink
Merge pull request #253 from gitroomhq/feat-better-registration-errors
Browse files Browse the repository at this point in the history
feat: Better registration errors
  • Loading branch information
nevo-david authored Sep 24, 2024
2 parents ec0259d + 2e24c78 commit 9e2c825
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions apps/frontend/src/components/auth/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ export function Register() {
);
}

function getHelpfulReasonForRegistrationFailure(httpCode: number) {
switch (httpCode) {
case 400:
return 'Email already exists';
case 404:
return 'Your browser got a 404 when trying to contact the API, the most likely reasons for this are the NEXT_PUBLIC_BACKEND_URL is set incorrectly, or the backend is not running.';
}

return 'Unhandled error: ' + httpCode;
}

export function RegisterAfter({
token,
provider,
Expand Down Expand Up @@ -97,23 +108,31 @@ export function RegisterAfter({

const onSubmit: SubmitHandler<Inputs> = async (data) => {
setLoading(true);
const register = await fetchData('/auth/register', {

await fetchData('/auth/register', {
method: 'POST',
body: JSON.stringify({ ...data }),
});
if (register.status === 400) {
form.setError('email', {
message: 'Email already exists',
});

}).then((response) => {
setLoading(false);
}

fireEvents('register');

if (register.headers.get('activate')) {
router.push('/auth/activate');
}
if (response.status === 200) {
fireEvents('register')

if (response.headers.get('activate') === "true") {
router.push('/auth/activate');
} else {
router.push('/auth/login');
}
} else {
form.setError('email', {
message: getHelpfulReasonForRegistrationFailure(response.status),
});
}
}).catch(e => {
form.setError("email", {
message: 'General error: ' + e.toString() + '. Please check your browser console.',
});
})
};

return (
Expand Down

0 comments on commit 9e2c825

Please sign in to comment.