Skip to content

Commit

Permalink
Promiseを使っていた所をasync/awaitに書き直しました
Browse files Browse the repository at this point in the history
  • Loading branch information
えむけー committed Jan 16, 2018
1 parent 18c9660 commit 8183221
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 57 deletions.
11 changes: 5 additions & 6 deletions models/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
const loader = require('./sequelize-loader')
const User = require('./user')

loader.database.sync()
.then(() =>
loader.database.close()
)
.then(() => {
const sync = async () => {
await loader.database.sync()
await loader.database.close()
console.log('db sync done!')
})
}

sync()
6 changes: 6 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
const loader = require('./sequelize-loader')
const Sequelize = loader.Sequelize
const bcrypt = require('bcrypt')

const User = loader.database.define('users', {
id: {
Expand All @@ -27,5 +28,10 @@ const User = loader.database.define('users', {
timestamps: true
})

User.prototype.verifyPassword = function(password) {
console.log(`password_hash=[${this.password_hash}]`)
return bcrypt.compare(password, this.password_hash)
}

module.exports = User

19 changes: 8 additions & 11 deletions routes/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ router.get('/', (req, res) => {
res.render('login')
})

router.post('/', (req, res) => {
User.findOne({
router.post('/', async (req, res) => {
const user = await User.findOne({
where: { email: req.body.email }
})
.then(user => {
if (bcrypt.compareSync(req.body.password, user.password_hash)) {
// パスワード一致
} else {
// パスワード不一致
}
res.redirect('/')
})
if (await bcrypt.compare(req.body.password, user.password_hash)) {
// パスワード一致
} else {
// パスワード不一致
}
res.redirect('/')
})

module.exports = router

29 changes: 10 additions & 19 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,25 @@ const router = express.Router()

const User = require('../models/user.js')

router.get('/', (req, res) => {
User.findAll({
router.get('/', async (req, res) => {
const users = await User.findAll({
order: [['id', 'ASC']]
})
.then(users => {
res.render('users/index', { users: users })
})
res.render('users/index', { users: users })
})

router.get('/new', (req, res) => {
res.render('users/new')
})

router.post('/', (req, res) => {
hash(req.body.password)
.then(hashed_password =>
User.create({
name: req.body.username,
email: req.body.email,
password_hash: hashed_password
})
)
.then((user) => {
res.redirect('/users')
})
.catch(error => {
res.redirect('/error')
router.post('/', async (req, res) => {
const hashed_password = await hash(req.body.password)
await User.create({
name: req.body.username,
email: req.body.email,
password_hash: hashed_password
})
res.redirect('/users')
})

const hash = password => bcrypt.hash(password, 10)
Expand Down
41 changes: 20 additions & 21 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const app = require('../app')
const sequelize = require('../models/sequelize-loader').database
const User = require('../models/user')

before(() =>
before(() =>
sequelize.sync()
)

Expand All @@ -18,32 +18,30 @@ after(() =>

describe('/users', () => {

before(() =>
before(() =>
User.create({
email: 'test@gmail.com',
name: 'テストユーザー',
password_hash: bcrypt.hashSync('password', 10)
})
)

after(() =>
User.findAll({
after(async () => {
const users = await User.findAll({
where: { email: 'test@gmail.com' }
})
.then(users =>
Promise.all(users.map(user => user.destroy()))
)
)
return Promise.all(users.map(user => user.destroy()))
})

it('ユーザー一覧に表示される', () =>
it('ユーザー一覧に表示される', () =>
request(app)
.get('/users')
.expect(200)
.expect(/テストユーザー/)
)

it('アカウント作成でユーザーが登録される', () =>
request(app)
it('アカウント作成でユーザーが登録される', async () => {
await request(app)
.post('/users')
.send({
username: 'テストユーザー2',
Expand All @@ -52,15 +50,16 @@ describe('/users', () => {
})
.expect(302)
.expect('Location', '/users')
.then(() =>
User.findOne({ where: { email: 'test2@gmail.com' } })
)
.then(user => {
assert.equal(user.name, 'テストユーザー2')
assert.ok(bcrypt.compareSync('password', user.password_hash))
return user.destroy()
})
)
.then()
const user = await User.findOne({ where: { email: 'test2@gmail.com' } })
assert.equal(user.name, 'テストユーザー2')
assert.ok(bcrypt.compareSync('password', user.password_hash))
await user.destroy()
})

})
it('パスワードのチェック', async () => {
const user = await User.findOne({ where: {email: 'test@gmail.com'} })
assert.ok(await user.verifyPassword('password'))
})

})

0 comments on commit 8183221

Please sign in to comment.