Skip to content

Commit 3036f43

Browse files
fix(authentication): validação se o token pertence a usuário existente
1 parent 1a0e14c commit 3036f43

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

src/controllers/auth-controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { tokenValido } = require('../utils/authentication')
77

88
exports.checkAdm = async (req, res, next) => {
99
try {
10-
if (!tokenValido(req.headers)) {
10+
if (!await tokenValido(req.headers)) {
1111
return res.status(401).send({ message: constant.TOKEN_INVALID })
1212
}
1313
const tokenDecodificado = authService.verifyToken(req.headers.authorization)
@@ -22,7 +22,7 @@ exports.checkAdm = async (req, res, next) => {
2222

2323
exports.checkToken = async (req, res, next) => {
2424
try {
25-
if (!tokenValido(req.headers)) {
25+
if (!await tokenValido(req.headers)) {
2626
return res.status(401).send({ message: constant.TOKEN_INVALID })
2727
}
2828
next()

src/utils/authentication.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict'
22

33
const authService = require('../services/auth-service')
4+
const { existeUsuario } = require('../services/usuarios-service')
45

5-
function tokenValido ({ authorization }) {
6+
async function tokenValido ({ authorization }) {
67
if (authorization === undefined) return false
78

89
const semBearer = authorization.split(' ')[0] !== 'Bearer'
@@ -17,7 +18,7 @@ function tokenValido ({ authorization }) {
1718
return false
1819
}
1920

20-
return true
21+
return await existeUsuario({ email: tokenDecodificado.email, password: tokenDecodificado.password })
2122
}
2223

2324
module.exports = {

src/utils/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
EMAIL_JA_USADO: 'Este email já está sendo usado',
1212
NOME_JA_USADO: 'Já existe produto com esse nome',
1313
NECESSARIO_ADM: 'Rota exclusiva para administradores',
14-
TOKEN_INVALID: 'Token de acesso inexistente, inválido ou expirado',
14+
TOKEN_INVALID: 'Token de acesso ausente, inválido, expirado ou usuário do token não existe mais',
1515
LIMITE_1_CARRINHO: 'Não é permitido ter mais de 1 carrinho',
1616
IDPRODUTO_INVALIDO: 'Id de produto inexistente',
1717
ESTOQUE_INSUFICIENTE: 'Produto não possui quantidade suficiente',

0 commit comments

Comments
 (0)