diff --git a/src/app.js b/src/app.js index 47a3c23..1c00a92 100644 --- a/src/app.js +++ b/src/app.js @@ -1,5 +1,5 @@ const express = require('express'); -const { userController } = require('./controllers'); +const { loginController, userController } = require('./controllers'); const { error, auth } = require('./middlewares'); // ... @@ -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); diff --git a/src/controllers/index.js b/src/controllers/index.js index 5cfedab..aa86094 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -1,5 +1,7 @@ const userController = require('./userController'); +const loginController = require('./loginController'); module.exports = { userController, + loginController, }; \ No newline at end of file diff --git a/src/controllers/loginController.js b/src/controllers/loginController.js new file mode 100644 index 0000000..dac9c3a --- /dev/null +++ b/src/controllers/loginController.js @@ -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 \ No newline at end of file diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 74fa846..26d3e47 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -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 }); @@ -42,7 +30,6 @@ const getById = async (req, res, next) => { }; module.exports = { - login, create, getAll, getById, diff --git a/src/services/index.js b/src/services/index.js index 1a67ab2..c157cac 100644 --- a/src/services/index.js +++ b/src/services/index.js @@ -1,5 +1,6 @@ const userService = require('./userService'); - +const loginService = require('./loginService'); module.exports = { userService, + loginService, }; diff --git a/src/services/loginService.js b/src/services/loginService.js new file mode 100644 index 0000000..ba0396d --- /dev/null +++ b/src/services/loginService.js @@ -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 \ No newline at end of file diff --git a/src/services/userService.js b/src/services/userService.js index ababeac..e41727c 100644 --- a/src/services/userService.js +++ b/src/services/userService.js @@ -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 = { @@ -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 }) => { @@ -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 () => { @@ -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, diff --git a/src/services/validations/createUserValidation.js b/src/services/validations/createUserValidation.js index 763f96c..96808e6 100644 --- a/src/services/validations/createUserValidation.js +++ b/src/services/validations/createUserValidation.js @@ -3,6 +3,6 @@ const { createUserSchema } = require('../../utils/schemas'); const validateCreateUser = (body) => { const { error } = createUserSchema.validate(body); return error; -} +}; module.exports = validateCreateUser; diff --git a/src/services/validations/loginValidation.js b/src/services/validations/loginValidation.js index 7e0d08e..d03bee1 100644 --- a/src/services/validations/loginValidation.js +++ b/src/services/validations/loginValidation.js @@ -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; diff --git a/src/utils/schemas.js b/src/utils/schemas.js index a79164f..68803df 100644 --- a/src/utils/schemas.js +++ b/src/utils/schemas.js @@ -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(), });