Skip to content

Implementa a sessão#70

Merged
alanhoff merged 3 commits intobackendfrom
feat/sessions
Nov 16, 2016
Merged

Implementa a sessão#70
alanhoff merged 3 commits intobackendfrom
feat/sessions

Conversation

@alanhoff
Copy link
Member

No description provided.

@alanhoff
Copy link
Member Author

@thebergamo RFR

@codecov-io
Copy link

codecov-io commented Nov 15, 2016

Current coverage is 96.79% (diff: 96.07%)

Merging #70 into backend will increase coverage by 0.65%

@@            backend        #70   diff @@
==========================================
  Files            15         21     +6   
  Lines           311        468   +157   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits            299        453   +154   
- Misses           12         15     +3   
  Partials          0          0          

Powered by Codecov. Last update aabd929...e7edd57

circle.yml Outdated
environment:
DATABASE_URL: mysql://ubuntu@localhost/circle_test
NODE_ENV: test
COOKIE_SECRET: here_be_dragrons
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dragrons?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frixed

Copy link
Contributor

@brunops brunops left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor comments, pretty good overall 👍

lib/session.js Outdated
const session = cookieSession({
name: 'session',
keys: [process.env.COOKIE_SECRET],
maxAge: 168 * 60 * 60 * 1000, // Uma semana
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não seria mais claro 24 * 7 ao invés de 168? (mas na real não ligo muito pra isso)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concordo com o @brunops se ficar 24 * 7 facilita a leitura

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, mas botei o dia na frente das horas :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

radical

lib/session.js Outdated
// Caso o acesso seja restrito à usuários logados precisamos verificar
// se a sessão foi lida com sucesso
if ((!req.session || !req.session.user_id) && config.restrict) {
res.status(401).send({ error: 'Unauthorized' })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dá pra remover o else se adicionar um return aqui

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


// Verifica se a senha está ok e seta a sessão
if (yield user.compare(req.body.password)) {
req.session.user_id = user.id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camelCase? userId?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No caso, quando são objetos é melhor usar com o _ mesmo @brunops

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quando alguma referência a uma coluna no banco de dados aí usa underscore, caso contrário camelCase mesmo.

exports.cookie = (username, password) => request(server)
.post('/sessions')
.send({ username, password })
.then(res => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

se quiser ser fancy res => res.header['set-cookie'].map(e => e.split(';')[0]).join(';')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

type: err.details[0].type
})
} else if (err.isBoom) {
res.status(err.output.statusCode).send(err.output.payload)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use um return aqui no lugar do else. Nos casos acima também acredito que isso ira facilitar a leitura.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

lib/session.js Outdated
const session = cookieSession({
name: 'session',
keys: [process.env.COOKIE_SECRET],
maxAge: 168 * 60 * 60 * 1000, // Uma semana
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concordo com o @brunops se ficar 24 * 7 facilita a leitura

package.json Outdated
"start": "node index.js"
"start": "node index.js",
"migrate": "npm run knex migrate:latest",
"reset": "node -e \"require('./lib/db').dropDatabase().then(() => process.exit(0), err => { console.error(err.stack); process.exit(1) })\"; npm run migrate"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acredito que valha a pena mover isso para um arquivo e referenciar o mesmo aqui? Talvez criar um diretório scripts o mesmo acredito que valha pro test-cov

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Não acho que o test-cov deva ir para um script visto que é um punhado de argumentos, sem lógica de programação !== script.


// Verifica se a senha está ok e seta a sessão
if (yield user.compare(req.body.password)) {
req.session.user_id = user.id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No caso, quando são objetos é melhor usar com o _ mesmo @brunops

if (yield user.compare(req.body.password)) {
req.session.user_id = user.id
} else {
throw Boom.badData('Sua senha está incorreta')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acho que faz mais sentido checar se não tem password e devolver um throw. Dai estar a sessão caso tudo esteja correto e seguir a vida.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thebergamo não entendi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!yeld user.compare(req.body.password)) {
    throw Boom.badData('Sua senha está incorreta')
}

Tipo isso ^

@alanhoff
Copy link
Member Author

@thebergamo RFR

@alanhoff alanhoff merged commit d957869 into backend Nov 16, 2016
@alanhoff alanhoff deleted the feat/sessions branch November 16, 2016 01:34
@thebergamo thebergamo mentioned this pull request Nov 16, 2016
@thebergamo thebergamo added this to the Site on AIR! milestone Nov 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants