Skip to content

Commit

Permalink
feat: req05
Browse files Browse the repository at this point in the history
  • Loading branch information
dopimentel committed Feb 21, 2024
1 parent 817d4a4 commit b60a841
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const { userController } = require('./controllers');
const { error } = require('./middlewares');
const { error, auth } = require('./middlewares');

// ...

Expand All @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
26 changes: 26 additions & 0 deletions src/middlewares/auth.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions src/middlewares/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const error = require('./error');
const auth = require('./auth');

module.exports = {
error,
auth,
};
7 changes: 7 additions & 0 deletions src/services/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
2 changes: 1 addition & 1 deletion src/utils/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit b60a841

Please sign in to comment.