From b60a8417ade343e6adf4c6a8fffa9a35f19ee326 Mon Sep 17 00:00:00 2001 From: Marcos Pimentel Date: Thu, 14 Sep 2023 23:46:19 -0300 Subject: [PATCH] feat: req05 --- src/app.js | 4 ++-- src/controllers/userController.js | 6 ++++++ src/middlewares/auth.js | 26 ++++++++++++++++++++++++++ src/middlewares/index.js | 2 ++ src/services/userService.js | 7 +++++++ src/utils/schemas.js | 2 +- 6 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/middlewares/auth.js diff --git a/src/app.js b/src/app.js index 0207b11..38170f9 100644 --- a/src/app.js +++ b/src/app.js @@ -1,6 +1,6 @@ const express = require('express'); const { userController } = require('./controllers'); -const { error } = require('./middlewares'); +const { error, auth } = require('./middlewares'); // ... @@ -15,7 +15,7 @@ app.use(express.json()); app.post('/login', userController.login); app.post('/user', userController.create); - +app.get('/user', auth, userController.getAll); app.use(error); diff --git a/src/controllers/userController.js b/src/controllers/userController.js index b3e0114..8938766 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -25,7 +25,13 @@ const create = async (req, res, next) => { res.status(response.status).json({ token: response.token}); } +const getAll = async (req, res, next) => { + const users = await userService.getAll(); + res.status(200).json(users); +} + module.exports = { login, create, + getAll, }; diff --git a/src/middlewares/auth.js b/src/middlewares/auth.js new file mode 100644 index 0000000..40dba1f --- /dev/null +++ b/src/middlewares/auth.js @@ -0,0 +1,26 @@ +const jwt = require('jsonwebtoken'); + +const { JWT_SECRET } = process.env; +const extractToken = (authorization) => authorization.split(' ')[1]; + +const auth = (req, _res, next) => { + const authorization = req.header('Authorization'); + console.log(authorization); + if (!authorization) { + const err = new Error('Token not found'); + err.status = 401; + return next(err); + } + try { + const token = extractToken(authorization); + const { email } = jwt.verify(token, JWT_SECRET); + req.email = email; + } catch (err) { + const newErr = new Error('Expired or invalid token'); + newErr.status = 401; + return next(newErr); + } + next(); +}; + +module.exports = auth; \ No newline at end of file diff --git a/src/middlewares/index.js b/src/middlewares/index.js index a8caa44..5088819 100644 --- a/src/middlewares/index.js +++ b/src/middlewares/index.js @@ -1,5 +1,7 @@ const error = require('./error'); +const auth = require('./auth'); module.exports = { error, + auth, }; \ No newline at end of file diff --git a/src/services/userService.js b/src/services/userService.js index e38eab9..5ec4ef5 100644 --- a/src/services/userService.js +++ b/src/services/userService.js @@ -50,9 +50,16 @@ const create = async ({ displayName, email, password, image }) => { return { status: 201, token } }; +const getAll = async () => { + const users = await User.findAll( + { attributes: { exclude: ['password'] } }, + ); + return users; +}; module.exports = { createToken, login, create, + getAll, }; \ No newline at end of file diff --git a/src/utils/schemas.js b/src/utils/schemas.js index e8cd7e4..a79164f 100644 --- a/src/utils/schemas.js +++ b/src/utils/schemas.js @@ -19,7 +19,7 @@ const createUserSchema = Joi.object({ 'any.min': 'displayName must be at least 8 characters long', 'any.required': 'Some required fields are missing', }), - email: Joi.string().regex(/^[a-z0-9.]+@[a-z0-9]+\.[a-z]+(\.[a-z]+)?$/i).required() + 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.pattern.base': '"email" must be a valid email',