O objetivo da criação desta api é colocar em prática o curso da DH
Foi utilizado o conceito de classes, e orientação a objeto
- NodeJS
- ExpressJS
- mySQL
- javaScript
- JWT
- CryptoJS
router.get('/list', userController.list);
[
{
"id": 40,
"Name": "LARISSA",
"Email": "MAIL26@DOMAIN.COM.BR"
},
{
"id": 42,
"Name": "LENES",
"Email": "LENES@DOMAIN.COM.BR"
},
{
"id": 41,
"Name": "LUANA",
"Email": "LUANA@DOMAIN.COM.BR"
},
{
"id": 44,
"Name": "LUIZA",
"Email": "LUIZA@DOMAIN.COM.BR"
},
]
router.get('/list', userController.list);
[
{
"id": 40,
"Name": "LARISSA",
"Email": "MAIL26@DOMAIN.COM.BR"
},
{
"id": 42,
"Name": "LENES",
"Email": "LENES@DOMAIN.COM.BR"
}
]
router.get('/:id', userController.user);
[
{
"id": 2,
"Name": "LARA",
"Email": "MAIL2@DOMAIN.COM"
}
]
router.post('/save', userController.save);
{
"data": {
"id": 44,
"Name": "LUIZA",
"Email": "LUIZA@DOMAIN.COM.BR"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImlkIjo0NCwiTmFtZSI6IkxVSVpBIiwiRW1haWwiOiJMVUlaQUBET01BSU4uQ09NLkJSIn0sImlhdCI6MTU5MTIwNzg3OSwiZXhwIjoxNTkxODEyNjc5fQ.iN8GIwzlbu84Ezx9YrdyTAWGYCrttgFLagRAaPBFgL8"
}
router.put('/edit/:id', userController.edit);
{
"fieldCount": 0,
"affectedRows": 1,
"insertId": 0,
"info": "Rows matched: 1 Changed: 1 Warnings: 0",
"serverStatus": 2,
"warningStatus": 0,
"changedRows": 1
}
router.delete('/delete/:id', userController.delete);
{
"fieldCount": 0,
"affectedRows": 1,
"insertId": 0,
"info": "",
"serverStatus": 2,
"warningStatus": 0
}
router.get('/user/:name', userController.find);
[
{
"id": 3,
"Name": "ALINE",
"Email": "MAIL@DOMAIN.COM.BR"
},
{
"id": 40,
"Name": "ALINE",
"Email": "MAIL2@DOMAIN.COM"
}
]
router.post('/register', userController.save);
router.post('/login', userController.login);
router.get('/credential', function (req, res) {
res.render('index');
});
You'll find details about it on: public/javascripts/createCredentials.js The credential needs to be in headers on request
{ email: userName.value, password: password.value }
Exemplo de criptografia utilizando cryptoJS:
let user = { email: userName.value, password: password.value };
let credential = CryptoJS.AES.encrypt(JSON.stringify(user), 'credential').toString();
inputCredential.value = credential;
headers:{credential: 'U2FsdGVkX1/82buCFBVVnuXrxSBQMlOulgmwQ4/Xr0uHgKiF6Uhp0c9vT7r70XdpX9JaaaEImD1VQJ+eQ7EwWY+Yr7uj6yNGfotZBoGuwbI='}
//VALIDAR TOKEN
//--------------------------------------------------------
verifyToken: (req, res, next) => {
try {
const NOW = Date.now();
if (!req.headers.authorization) {
res.status(401).json({ error: { message:'Token inválido'}});
} else {
//substring para tirar o "Bearer "
let token = req.headers.authorization.substring(7);
let decode = jwt.verify(token, process.env.SECRET_KEY);
if (decode.data.id != undefined) {
//Verificar se o token expirou
if (NOW > decode.exp * 1000) {
res.status(401).json({ error: { message: 'Token expirado' } });
} else {
next();
}
} else {
res.status(401).json({ error: { message: 'Token inválido' } });
}
}
} catch (error) {
res.status(401).json({ error });
}
},
app.use('/', indexRouter);
app.use(Auth.verifyToken);
app.use('/users', usersRouter);
git clone https://github.com/ALESSANDROLMENEZES/restApi.git
npm install
- configurar seu arquivo .env com as informações de seu ambiente
- criar um banco de dados com as seguintes colunas:
npm start
- Acessar via postman ou similar
- As rotas autenticadas requerem um token, faça o cadastro na rota publica e informe o token no headers