Skip to content

Commit

Permalink
refatoração - lint
Browse files Browse the repository at this point in the history
  • Loading branch information
dopimentel committed Feb 21, 2024
1 parent 3bccc59 commit d9d4903
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require('express');
const { userController } = require('./controllers');
const { loginController, userController } = require('./controllers');
const { error, auth } = require('./middlewares');

// ...
Expand All @@ -13,7 +13,7 @@ app.get('/', (_request, response) => {

app.use(express.json());

app.post('/login', userController.login);
app.post('/login', loginController);
app.post('/user', userController.create);
app.get('/user', auth, userController.getAll);
app.get('/user/:id', auth, userController.getById);
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const userController = require('./userController');
const loginController = require('./loginController');

module.exports = {
userController,
loginController,
};
15 changes: 15 additions & 0 deletions src/controllers/loginController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { loginService } = require('../services');

const loginController = async (req, res, next) => {
const { email, password } = req.body;
const response = await loginService( { email, password });
if (response.error) return next(response.error);
if (response.status === 400) {
const err = new Error(response.message);
err.status = response.status;
return next(err);
}
res.status(200).json({ token: response.token });
};

module.exports = loginController
13 changes: 0 additions & 13 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
const { userService } = require('../services');

const login = async (req, res, next) => {
const { email, password } = req.body;
const response = await userService.login( { email, password });
if (response.error) return next(response.error);
if (response.status === 400) {
const err = new Error(response.message);
err.status = response.status;
return next(err);
}
res.status(200).json({ token: response.token });
};

const create = async (req, res, next) => {
const { displayName, email, password, image } = req.body;
const response = await userService.create({ displayName, email, password, image });
Expand Down Expand Up @@ -42,7 +30,6 @@ const getById = async (req, res, next) => {
};

module.exports = {
login,
create,
getAll,
getById,
Expand Down
3 changes: 2 additions & 1 deletion src/services/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const userService = require('./userService');

const loginService = require('./loginService');
module.exports = {
userService,
loginService,
};
24 changes: 24 additions & 0 deletions src/services/loginService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { loginValidation } = require('./validations');
const { findByEmail, createToken } = require('./userService');

const loginService = async ({ email, password }) => {
const error = loginValidation({ email, password });
if (error) return { error };

const user = await findByEmail(email);
if (!user || user.password !== password) {
return {
status: 400,
message: 'Invalid fields',
};
}

const token = createToken({ email });

return {
status: 200,
token,
};
};

module.exports = loginService
36 changes: 10 additions & 26 deletions src/services/userService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const jwt = require('jsonwebtoken');
const { User } = require('../models');
const { loginValidation, createUserValidation } = require('./validations');
const { createUserValidation } = require('./validations');

const { JWT_SECRET } = process.env;
const jwtConfig = {
Expand All @@ -13,24 +13,9 @@ const createToken = (payload) => {
return token;
};

const login = async ({ email, password }) => {
const error = loginValidation({ email, password });
if (error) return { error };

const findByEmail = async (email) => {
const user = await User.findOne({ where: { email } });
if (!user || user.password !== password) {
return {
status: 400,
message: 'Invalid fields',
};
}

const token = createToken({ email })

return {
status: 200,
token,
};
return user;
};

const create = async ({ displayName, email, password, image }) => {
Expand All @@ -43,11 +28,9 @@ const create = async ({ displayName, email, password, image }) => {
message: 'User already registered',
};
}

const user = await User.create({ displayName, email, password, image });
const token = createToken({ email })

return { status: 201, token }
await User.create({ displayName, email, password, image });
const token = createToken({ email });
return { status: 201, token };
};

const getAll = async () => {
Expand All @@ -58,15 +41,16 @@ const getAll = async () => {
};

const getById = async (id) => {
const user = await User.findByPk(id
, { attributes: { exclude: ['password'] } },
const user = await User.findByPk(
id,
{ attributes: { exclude: ['password'] } },
);
return user;
};

module.exports = {
createToken,
login,
findByEmail,
create,
getAll,
getById,
Expand Down
2 changes: 1 addition & 1 deletion src/services/validations/createUserValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ const { createUserSchema } = require('../../utils/schemas');
const validateCreateUser = (body) => {
const { error } = createUserSchema.validate(body);
return error;
}
};

module.exports = validateCreateUser;
5 changes: 2 additions & 3 deletions src/services/validations/loginValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ const { loginSchema } = require('../../utils/schemas');

const validateLogin = (body) => {
const { error } = loginSchema.validate(body);
console.log(error)
return error
}
return error;
};

module.exports = validateLogin;
23 changes: 12 additions & 11 deletions src/utils/schemas.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
const Joi = require('joi');

const requiredMessage = 'Some required fields are missing';
const loginSchema = Joi.object({
email: Joi.string().required()
.messages({
'string.empty': 'Some required fields are missing',
'any.required': 'Some required fields are missing',
'string.empty': requiredMessage,
'any.required': requiredMessage,
}),
password: Joi.string().required().messages({
'string.empty': 'Some required fields are missing',
'any.required': 'Some required fields are missing',
'string.empty': requiredMessage,
'any.required': requiredMessage,
}),
});

const createUserSchema = Joi.object({
displayName: Joi.string().min(8).required()
.messages({
'string.empty': 'Some required fields are missing',
'string.empty': requiredMessage,
'any.min': 'displayName must be at least 8 characters long',
'any.required': 'Some required fields are missing',
'any.required': requiredMessage,
}),
email: Joi.string().regex(/^[a-z0-9.]+@[a-z0-9]+\.[a-z]+(\.[a-z]+)?$/i).required() /// regex [^@ \t\r\n]+@[^@ \t\r\n]+\.[^@ \t\r\n]+/
.messages({
'string.empty': 'Some required fields are missing',
'string.empty': requiredMessage,
'string.pattern.base': '"email" must be a valid email',
'any.required': 'Some required fields are missing',
'any.required': requiredMessage,
}),
password: Joi.string().min(6).required()
.messages({
'string.empty': 'Some required fields are missing',
'string.empty': requiredMessage,
'any.min': 'password must be at least 6 characters long',
'any.required': 'Some required fields are missing',
'any.required': requiredMessage,
}),
image: Joi.string()
image: Joi.string(),

});

Expand Down

0 comments on commit d9d4903

Please sign in to comment.