Skip to content

Commit

Permalink
chore: use fastify validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarneo committed Mar 9, 2024
1 parent b7c01ac commit 6736753
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 81 deletions.
12 changes: 6 additions & 6 deletions src/client/routes/signup/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ const SignUp = () => {
const onSignUp = async (values) => {
const data = await signUp(values.email, values.username, values.password);

if (data.statusCode === 403) {
setError(data.error);
if ([400, 403].indexOf(data.statusCode) > -1) {
setError(data.message);

setSuccess(false);

return;
}

if (data.error) {
if (data.type && data.message) {
form.setErrors({
username: data.type == 'username' ? data.error : '',
password: data.type == 'password' ? data.error : '',
email: data.type == 'email' ? data.error : '',
username: data.type == 'username' ? data.message : '',
password: data.type == 'password' ? data.message : '',
email: data.type == 'email' ? data.message : '',
});

setSuccess(false);
Expand Down
2 changes: 0 additions & 2 deletions src/server/controllers/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import emailValidator from 'email-validator';
import { compare, hash } from '../helpers/password.js';
import prisma from '../services/prisma.js';

const PASSWORD_LENGTH = 5;

async function account(fastify) {
fastify.get(
'/',
Expand Down
156 changes: 83 additions & 73 deletions src/server/controllers/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,88 +27,98 @@ const PUBLIC_COOKIE_SETTINGS = {
};

async function authentication(fastify) {
fastify.post('/signup', async (request, reply) => {
const { email = '', username = '', password = '' } = request.body;

if (!emailValidator.validate(email)) {
return reply.code(403).send({
type: 'email',
error: `Your email: "${email}" is not valid.`,
});
}
fastify.post(
'/signup',
{
schema: {
body: {
type: 'object',
required: ['email', 'username', 'password'],
properties: {
email: { type: 'string' },
username: { type: 'string', minLength: 4, maxLength: 20 },
password: { type: 'string', minLength: 5, maxLength: 50 },
},
},
},
},
async (request, reply) => {
const { email, username, password } = request.body;

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 (!emailValidator.validate(email)) {
return reply.code(400).send({
type: 'email',
message: `Your email: "${email}" is not valid.`,
});
}

if (password.length < PASSWORD_LENGTH) {
return reply.code(403).send({
type: 'password',
error: `Password has to be longer than ${PASSWORD_LENGTH} characters`,
if (!validUsername.test(username)) {
return reply.code(400).send({
type: 'username',
message: `Username can only contain these characters. [A-Za-z0-9_-]`,
});
}

const userExist = await prisma.user.findFirst({ where: { username } });
if (userExist) {
return reply
.code(403)
.send({ type: 'username', message: `This username has already been taken.` });
}

const emailExist = await prisma.user.findFirst({ where: { email } });
if (emailExist) {
return reply
.code(403)
.send({ type: 'email', message: `This email has already been registered.` });
}

const userPassword = await hash(password);

const user = await prisma.user.create({
data: {
username,
email,
password: userPassword,
role: 'user',
},
});
}

const userExist = await prisma.user.findFirst({ where: { username } });
if (userExist) {
return reply
.code(403)
.send({ type: 'username', error: `This username has already been taken.` });
}
if (!user) {
return reply.code(400).send({
message:
'Something happened while creating a new user. Please try again later.',
});
}

const emailExist = await prisma.user.findFirst({ where: { email } });
if (emailExist) {
return reply
.code(403)
.send({ type: 'email', error: `This email has already been registered.` });
}
const sacredToken = await reply.jwtSign(
{
username: user.username,
email: user.email,
user_id: user.id,
},
{ expiresIn: '7d' } // expires in seven days
);

const userPassword = await hash(password);
const expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + 6);

const user = await prisma.user.create({
data: {
username,
email,
password: userPassword,
role: 'user',
},
});
const publicToken = Buffer.from(
JSON.stringify({
username: user.username,
expirationDate: expirationDate,
})
).toString('base64');

if (!user) {
return reply.code(403).send({
error: 'Something happened while creating a new user. Please try again later.',
});
reply
.setCookie(COOKIE_KEY, sacredToken, SACRED_COOKIE_SETTINGS)
.setCookie(COOKIE_KEY_PUBLIC, publicToken, PUBLIC_COOKIE_SETTINGS)
.code(200)
.send({
username: user.username,
});
}

const sacredToken = await reply.jwtSign(
{
username: user.username,
email: user.email,
user_id: user.id,
},
{ expiresIn: '7d' } // expires in seven days
);

const expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + 6);

const publicToken = Buffer.from(
JSON.stringify({
username: user.username,
expirationDate: expirationDate,
})
).toString('base64');

reply
.setCookie(COOKIE_KEY, sacredToken, SACRED_COOKIE_SETTINGS)
.setCookie(COOKIE_KEY_PUBLIC, publicToken, PUBLIC_COOKIE_SETTINGS)
.code(200)
.send({
username: user.username,
});
});
);

fastify.post('/signin', async (request, reply) => {
const { username = '', password = '' } = request.body;
Expand Down

0 comments on commit 6736753

Please sign in to comment.