-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(produtos): implementado rotas restantes e autenticação
- Loading branch information
1 parent
00352a8
commit 85c6738
Showing
9 changed files
with
125 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict' | ||
|
||
const authService = require('../services/auth-service') | ||
const constant = require('../utils/constants') | ||
const usuariosService = require('../services/usuarios-service') | ||
const { tokenValido } = require('../utils/authentication') | ||
|
||
exports.checkAdm = async (req, res, next) => { | ||
try { | ||
if (!tokenValido(req.headers)) { | ||
return res.status(401).send({ message: constant.TOKEN_INVALID }) | ||
} | ||
const tokenDecodificado = authService.verifyToken(req.headers.authorization) | ||
if (!await usuariosService.usuarioEhAdministrador(tokenDecodificado)) { | ||
return res.status(403).send({ message: constant.NECESSARIO_ADM }) | ||
} | ||
next() | ||
} catch (error) { | ||
res.status(500).send({ message: constant.INTERNAL_ERROR, error }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
'use strict' | ||
|
||
const { verifyToken } = require('./utils/token.js') | ||
const authService = require('../services/auth-service') | ||
|
||
function autenticacao (req, res) { | ||
if (req.headers.authorization === undefined) { | ||
res.status(401).json({ message: 'Autenticação necessária' }) | ||
return | ||
} | ||
if (req.headers.authorization.split(' ')[0] !== 'Bearer') { | ||
res.status(401).json({ message: 'Tipo de autenticação deve ser Bearer' }) | ||
return | ||
} | ||
const token = req.headers.authorization.split(' ')[1] | ||
if (token === undefined) { | ||
res.status(401).json({ message: 'Token de acesso vazio' }) | ||
return | ||
function tokenValido ({ authorization }) { | ||
if (authorization === undefined) return false | ||
|
||
const semBearer = authorization.split(' ')[0] !== 'Bearer' | ||
const semToken = authorization.split(' ')[1] === undefined | ||
|
||
if (semBearer || semToken) { | ||
return false | ||
} | ||
if (verifyToken(token) instanceof Error) { | ||
res.status(401).json({ message: 'Token de acesso não é válido' }) | ||
|
||
const tokenDecodificado = authService.verifyToken(authorization) | ||
if(tokenDecodificado.email === undefined || tokenDecodificado.password === undefined) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
module.exports = { | ||
autenticacao | ||
tokenValido | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.