Skip to content

Commit

Permalink
fix: how we handle the sign up error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarneo committed Aug 19, 2022
1 parent ce640c2 commit 8dbea25
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/client/routes/signup/index.js
Expand Up @@ -14,7 +14,7 @@ const Secret = () => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [email, setEmail] = useState('');
const [error, setError] = useState(null);
const [error, setError] = useState({});
const [token, setTokenState] = useState('');
const [success, setSuccess] = useState(false);

Expand Down Expand Up @@ -42,14 +42,14 @@ const Secret = () => {
const data = await signUp(email, username, password);

if (data.error) {
setError(data.error);
setError(data);
setSuccess(false);

return;
}

setTokenState(data.token);
setError(null);
setError({});
setSuccess(true);
};

Expand All @@ -70,7 +70,7 @@ const Secret = () => {
value={email}
onChange={onEmailChange}
required
error={error}
error={error?.type === 'email' ? error?.error : ''}
/>

<TextInput
Expand All @@ -79,7 +79,7 @@ const Secret = () => {
value={username}
onChange={onUsernameChange}
required
error={error}
error={error?.type === 'username' ? error?.error : ''}
/>

<PasswordInput
Expand All @@ -88,7 +88,7 @@ const Secret = () => {
value={password}
onChange={onPasswordChange}
required
error={error}
error={error?.type === 'password' ? error?.error : ''}
/>

<Button
Expand Down
7 changes: 6 additions & 1 deletion src/server/controllers/authentication.js
Expand Up @@ -20,23 +20,28 @@ async function authentication(fastify) {

if (!emailValidator.validate(email)) {
return reply.code(403).send({
type: 'email',
error: `Your email: "${email}" is not valid.`,
});
}
if (!validUsername.test(username) || username.length < USERNAME_LENGTH) {
return reply.code(403).send({
type: 'username',
error: `Username has to be longer than ${USERNAME_LENGTH}, and can only contain these characters. [A-Za-z0-9_-]`,
});
}

if (password.length < PASSWORD_LENGTH) {
return reply.code(403).send({
type: 'password',
error: `Password has to be longer than ${PASSWORD_LENGTH} characters`,
});
}

if (await redis.getUser(username)) {
return reply.code(403).send({ error: `This username has already been taken.` });
return reply
.code(403)
.send({ type: 'username', error: `This username has already been taken.` });
}

const userPassword = await hash(validator.escape(password));
Expand Down

0 comments on commit 8dbea25

Please sign in to comment.