Skip to content

Commit

Permalink
Configurando variaveis de ambiente
Browse files Browse the repository at this point in the history
  • Loading branch information
pretodev committed Nov 22, 2023
1 parent 1e0af26 commit 06cb58e
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 13 deletions.
9 changes: 9 additions & 0 deletions .env.exemplo
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PORT=3000

DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=postgre
DB_NAME=gira

JWT_SECRET=secret
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
node_modules

.env
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Gira API

## Configuração

```
Defina o seu arquivo .env utilizando o .env.exemplo
```

## Documentação

Esta a API Rest para servir dados para plataforma Gira.
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.1",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.2",
"pg": "^8.11.3"
},
"devDependencies": {
"nodemon": "^3.0.1"
}
}
}
10 changes: 5 additions & 5 deletions src/conexao.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { Pool } = require('pg')

module.exports = new Pool({
host: 'localhost',
port: 5432,
user: 'postgres',
password: 'postgre',
database: 'gira',
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
})
3 changes: 1 addition & 2 deletions src/controladores/usuario.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const conexao = require('../conexao')
const bcrypt = require('bcrypt')
const jwt = require('jsonwebtoken')
const segredo = require('../segredo')

const login = async (req, res) => {
const { email, senha } = req.body
Expand All @@ -15,7 +14,7 @@ const login = async (req, res) => {
if (!senhaVerificada) {
return res.status(400).json({ mensagem: 'Email e senha não confere' })
}
const token = jwt.sign({ id: usuario.id }, segredo, { expiresIn: '1d' })
const token = jwt.sign({ id: usuario.id }, process.env.JWT_SECRET, { expiresIn: '1d' })
return res.status(200).json({ token })
}

Expand Down
3 changes: 1 addition & 2 deletions src/filtros/verifica-usuario-logado.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const segredo = require('../segredo')
const jwt = require('jsonwebtoken')

module.exports = async (req, res, next) => {
Expand All @@ -8,7 +7,7 @@ module.exports = async (req, res, next) => {
return res.status(404).json({ mensagem: 'Token não encontrado' })
}
const token = authorization.replace('Bearer', '').trim()
const { id } = jwt.verify(token, segredo)
const { id } = jwt.verify(token, process.env.JWT_SECRET)
req.usuarioId = id
next()
} catch (error) {
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require('dotenv').config()

const express = require('express')
const rotas = require('./rotas')

const app = express()
app.use(express.json())
app.use(rotas)

app.listen(3000, () => {
app.listen(process.env.PORT, () => {
console.log('Servidor rodando em http://localhost:3000')
})
1 change: 0 additions & 1 deletion src/segredo.js

This file was deleted.

0 comments on commit 06cb58e

Please sign in to comment.