Created
October 21, 2017 10:21
-
-
Save farnetani/b81b6c4946a2fccf1c427e3fdabb8c49 to your computer and use it in GitHub Desktop.
API Restful com MongoDB e Express
This file contains hidden or 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
<p class="tip">**Nota Importante** | |
API em desenvolvimento. | |
</p> | |
## Repositório no github | |
[https://github.com/RaulSBunicenha/api-ts](https://github.com/RaulSBunicenha/api-ts) | |
``` | |
Instalando o repositório | |
$ git clone https://github.com/RaulSBunicenha/api-ts.git backend-farsoft | |
``` | |
## Informações importantes | |
- config > config-env.ts | |
``` | |
Local onde se configura os dados do banco de dados. | |
``` | |
## Parametros Obrigatórios | |
- Headers | |
Pegar a coluna key da collection: apps. Essa key já é gerada a primeira vez que se executa a aplicação. | |
``` | |
Headers | |
key value | |
x-app $2a$10$EY7Eejj1ynaPruPNOsMIAOXhegKciJ5OEasY3BHlUmH4QR9aSDuF. | |
``` | |
Também é importante passarmos o Contenty-Type como application/json | |
``` | |
Headers | |
key value | |
x-app $2a$10$EY7Eejj1ynaPruPNOsMIAOXhegKciJ5OEasY3BHlUmH4QR9aSDuF. | |
Contenty-Type application/json | |
``` | |
## Primeiro Acesso | |
É necessário se autenticar, ou seja, fazer o login para se obter o TOKEN e o REFRESH TOKEN para poder utilizar a API. | |
Então, devemos fazer uma requisição do tipo POST conforme abaixo: | |
``` | |
http://localhost:4200/sessao/login | |
``` | |
- Headers | |
``` | |
Headers | |
key value | |
x-app $2a$10$EY7Eejj1ynaPruPNOsMIAOXhegKciJ5OEasY3BHlUmH4QR9aSDuF. | |
Contenty-Type application/json | |
``` | |
- Body | |
``` | |
{ | |
"email": "[email protected]", | |
"senha": "demo.123" | |
} | |
``` | |
- Retorno do login (3) | |
Pode acontecer 3 retornos, ou seja, login e senha inválidos, o retorno do token se não tiver nenhuma sessão ativa deste login e senha ou o aviso de existência da sessão ativa. | |
1. Retorno de login e senha inválidos | |
``` | |
{ | |
"type": "error", | |
"message": "Senha incorreta!" | |
} | |
``` | |
2. Retorno do token (JSON) | |
``` | |
{ | |
"type": "response", | |
"result": { | |
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjU5ZTc0MDZmZDA5YTJmMzQzYzdjOGM4NiIsImV4cGlyYSI6MTUwODMzNjEwNDQ3Mn0.rO1H1pH6XHS-YX5i9nmZmtTA8WpmNy_SZ1MEAKXFDYU", | |
"refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjU5ZTc0MDZmZDA5YTJmMzQzYzdjOGM4NiIsImluaWNpYSI6MTUwODMzNTgwNDQ3Mn0.16v4JGxQUY-EVGYNbAXrllLSWLR5Z30gHTiarQJsT2w", | |
"expira": "2017-10-18T14:15:04.472Z", | |
"usuario": { | |
"_id": "59e7406fd09a2f343c7c8c86", | |
"nome": "Demo User", | |
"email": "[email protected]" | |
} | |
} | |
} | |
``` | |
3. Se já existir uma SESSÃO ATIVA | |
``` | |
{ | |
"type": "existe", | |
"result": { | |
"message": "Já existe uma sessao desse usuário", | |
"sessao": { | |
"_id": "59e75f90d09a2f343c7c8c9a", | |
"agent": { | |
"browser": { | |
"version": "61.0.3163.100", | |
"name": "Chrome" | |
}, | |
"engine": { | |
"version": "537.36", | |
"name": "WebKit" | |
}, | |
"os": { | |
"version": "10", | |
"name": "Windows" | |
}, | |
"ip": "127.0.0.1", | |
"device": "Desktop" | |
} | |
} | |
} | |
} | |
``` | |
Para resolver o retorno de sessão já existente, podemos fazer 2 ações: | |
3.1. Finalizar a sessão | |
Para fazer isso, temos que fazer uma requisição do tipo POST: | |
``` | |
http://localhost:4200/sessao/logout | |
``` | |
Precisamos também passar nos Headers os seguintes dados: | |
``` | |
Headers | |
key value | |
x-app $2a$10$EY7Eejj1ynaPruPNOsMIAOXhegKciJ5OEasY3BHlUmH4QR9aSDuF. | |
Contenty-Type application/json | |
``` | |
E no body devemos passar o token atual: | |
``` | |
{ | |
"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjU5ZTc0MDZmZDA5YTJmMzQzYzdjOGM4NiIsImV4cGlyYSI6MTUwODMyOTQ1MjA3MH0.BRowk_AiGJPg3TlxQXTrOs6uG6aLb_xrAvStUhXbng0" | |
} | |
``` | |
Poderemos ter 2 retornos: | |
3.1.1. Sessão finalizada | |
``` | |
{ | |
"type": "response", | |
"result": { | |
"message": "Sessão finalizada" | |
} | |
} | |
``` | |
3.1.2. Sessão não encontrada | |
``` | |
{ | |
"type": "error", | |
"message": "Sessão não encontrada" | |
} | |
``` | |
## Refresh Token | |
A sessão dura 10 minutos. Caso a sessão tenha ultrapassado esse tempo, será necessário fazermos uma requisição do tipo POST: | |
``` | |
http://localhost:4200/sessao/refresh | |
``` | |
Repassar nos Headers os parametros: | |
``` | |
x-app = <digitar a key da app> | |
Content-Type = application/json | |
``` | |
Em Body > raw passar o refresh token que veio com o login | |
``` | |
{ | |
"refresh":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjU5ZTc0MDZmZDA5YTJmMzQzYzdjOGM4NiIsImluaWNpYSI6MTUwODMzODIyNjMxNX0.uKWUqQd2vf2Zs77Zi4X60QKC6bcoT6WGogPY171aETA" | |
} | |
``` | |
O retorno deverá ser algo do tipo: | |
``` | |
{ | |
"type": "response", | |
"result": { | |
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjU5ZTc0MDZmZDA5YTJmMzQzYzdjOGM4NiIsImV4cGlyYSI6MTUwODMzOTMzNjEwMn0.4WNA3gppNLbkXumtf1Vazj2qnefSgQUfOJZqs2UR24s", | |
"refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjU5ZTc0MDZmZDA5YTJmMzQzYzdjOGM4NiIsImluaWNpYSI6MTUwODMzOTAzNjEwMn0.EwHZeVi_D2z6jzZvvLtTo29LsE88TYPJOqmC5kbofEs", | |
"expira": "2017-10-18T15:08:56.102Z", | |
"usuario": { | |
"_id": "59e7406fd09a2f343c7c8c86", | |
"nome": "Demo User", | |
"email": "[email protected]" | |
} | |
} | |
} | |
``` | |
## RESUMO | |
<p class="tip">**Nota Importante** | |
Usaremos a tabela de usuários para os exemplos. | |
</p> | |
## Inserindo um novo Usuário | CREATE | |
- http://localhost:4200/usuario | |
- Method: POST | |
- Headers: x-app = key em apps e Content-Type = application/json e x-access-token = <token obtido pelo login> | |
- Passar no Body (raw) os campos que serão inseridos: | |
``` | |
{ | |
"nome":"Arlei Ferreira Farnetani Junior", | |
"email":"[email protected]", | |
"senha":"Senha001*" | |
} | |
``` | |
O retorno (response) se tudo ocorrer bem: | |
```{ | |
"type": "response", | |
"result": { | |
"__v": 0, | |
"nome": "Ana Virginia Farnetani", | |
"email": "[email protected]", | |
"senha": "$2a$10$j8TZ2vNk.iRicX4h1MUUmumRt2RU7OfheQI7Cc9PIXqJ9rA43sQ2u", | |
"_id": "59e76a42d09a2f343c7c8cb1", | |
"criado_em": "2017-10-18T14:50:42.249Z", | |
"ativo": true | |
} | |
} | |
``` | |
## Listar Usuários | READ | |
- http://localhost:4200/usuario | |
- Method: GET | |
Passar nos Headers: | |
``` | |
Headers | |
key value | |
x-app $2a$10$EY7Eejj1ynaPruPNOsMIAOXhegKciJ5OEasY3BHlUmH4QR9aSDuF. | |
Contenty-Type application/json | |
x-access-token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjU5ZTc0MDZmZDA5YTJmMzQzYzdjOGM4NiIsImV4cGlyYSI6MTUwODMzODUyNjMxNX0.WH9V6xYdlMm8vSBllXNXvFoxDf2BabpKD5q8n9tsu4g | |
``` | |
Se tudo ocorrer bem, o retorno (response) será: | |
``` | |
{ | |
"type": "response", | |
"result": [ | |
{ | |
"_id": "59e76a42d09a2f343c7c8cb1", | |
"nome": "Ana Virginia Farnetani", | |
"email": "[email protected]", | |
"criado_em": "2017-10-18T14:50:42.249Z", | |
"ativo": true | |
}, | |
{ | |
"_id": "59e7406fd09a2f343c7c8c86", | |
"nome": "Demo User", | |
"email": "[email protected]", | |
"criado_em": "2017-10-18T11:52:15.056Z", | |
"ativo": true | |
}, | |
{ | |
"_id": "59e77eb40f8c421830e587c1", | |
"nome": "Ana Virginia Farnetani2", | |
"email": "[email protected]", | |
"criado_em": "2017-10-18T16:17:56.597Z", | |
"ativo": false | |
} | |
] | |
} | |
``` | |
## Editando um Usuário | UPDATE | |
Para editar um usuário basta fazer uma requisição do tipo PUT: | |
- http://localhost:4200/usuario/59e77eb40f8c421830e587c1 | |
-Method: PUT | |
- Headers: x-app = key em apps e Content-Type = application/json e x-access-token = <token capturado no login> | |
- Body (raw): | |
``` | |
{ | |
"nome":"Arlei Ferreira Farnetani Junior", | |
"email":"[email protected]", | |
"senha":"Junior01*" | |
} | |
``` | |
- Response | |
``` | |
{ | |
"type": "response", | |
"result": { | |
"nome": "Arlei Ferreira Farnetani Junior", | |
"email": "[email protected]", | |
"senha": "$2a$10$KbNg6tI759x7EM14VhpQ.OnOhQCiMqRa1Rq70fzaOPYdL6.QPZRN2", | |
"_id": "59e76a42d09a2f343c7c8cb1", | |
"alterado": 1508345913897 | |
} | |
} | |
``` | |
## Excluindo um Usuário | DELETE | |
Para EXCLUIR um usuário basta fazer uma requisição do tipo DELETE passando como parametro o id da tabela: | |
- http://localhost:4200/usuario/59e77eb40f8c421830e587c1 | |
-Method: DELETE | |
- Headers: x-app = key em apps e Content-Type = application/json e x-access-token = <token capturado no login> | |
- Response | |
``` | |
{ | |
"type": "response", | |
"result": { | |
"message": "Registro excluido!" | |
} | |
} | |
``` | |
## Erro de Sessao Recarregada (refresh) | |
Pode acontecer de se usar um token inválido / vencido, o retorno será: | |
``` | |
{ | |
"type": "refresh", | |
"message": "Sessao recarregada" | |
} | |
``` | |
Se o retorno for sessão expirada: | |
``` | |
{ | |
"type": "expirada", | |
"message": "Sua sessão expirou, por favor faça refresh" | |
} | |
``` | |
## Capturando o Token pelo Refresh Token | |
Fazer o passo do Refresh Token acima descrito e o retorno será: | |
``` | |
{ | |
"type": "response", | |
"result": { | |
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjU5ZTc0MDZmZDA5YTJmMzQzYzdjOGM4NiIsImV4cGlyYSI6MTUwODMzOTMzNjEwMn0.4WNA3gppNLbkXumtf1Vazj2qnefSgQUfOJZqs2UR24s", | |
"refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjU5ZTc0MDZmZDA5YTJmMzQzYzdjOGM4NiIsImluaWNpYSI6MTUwODMzOTAzNjEwMn0.EwHZeVi_D2z6jzZvvLtTo29LsE88TYPJOqmC5kbofEs", | |
"expira": "2017-10-18T15:08:56.102Z", | |
"usuario": { | |
"_id": "59e7406fd09a2f343c7c8c86", | |
"nome": "Demo User", | |
"email": "[email protected]" | |
} | |
} | |
} | |
``` | |
## RESUMO SESSAO | |
## Sessão | |
- http://localhost:4200/sessao/login | |
- Headers: x-app = key em apps e Content-Type = application/json | |
- Body (raw): | |
``` | |
{ | |
"email": "[email protected]", | |
"senha": "demo.123" | |
} | |
``` | |
## Refresh Token | |
- http://localhost:4200/sessao/refresh | |
- Headers: x-app = key em apps e Content-Type = application/json | |
- Body (raw): | |
``` | |
{ | |
"refresh":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjU5ZTc0MDZmZDA5YTJmMzQzYzdjOGM4NiIsImluaWNpYSI6MTUwODMzODIyNjMxNX0.uKWUqQd2vf2Zs77Zi4X60QKC6bcoT6WGogPY171aETA" | |
} | |
``` | |
## Logout | |
- http://localhost:4200/sessao/logout | |
- Headers: x-app = key em apps e Content-Type = application/json | |
- Body (raw): | |
``` | |
{ | |
"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjU5ZTc0MDZmZDA5YTJmMzQzYzdjOGM4NiIsImV4cGlyYSI6MTUwODMzOTMzNjEwMn0.4WNA3gppNLbkXumtf1Vazj2qnefSgQUfOJZqs2UR24s" | |
} | |
``` | |
## Continue | |
- http://localhost:4200/sessao/continue | |
- Headers: x-app = key em apps e Content-Type = application/json | |
- Body (raw): | |
``` | |
{ | |
"_id":"59e75f90d09a2f343c7c8c9a" | |
} | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment