Skip to content

Commit f618576

Browse files
committed
use async hashPassword/verifyPassword
Signed-off-by: BinotaLIU <me@binota.org>
1 parent ec206db commit f618576

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

bin/manage_users

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ async function createUser (argv) {
4343
}
4444

4545
const pass = getPass(argv, 'add')
46+
const hashedPass = await models.User.hashPassword(pass)
4647

4748
// Lets try to create, and check success
48-
const ref = await models.User.create({ email: argv['add'], password: pass })
49+
const ref = await models.User.create({ email: argv['add'], password: hashedPass })
4950
if (ref === undefined) {
5051
console.log(`Could not create user with email ${argv['add']}`)
5152
process.exit(1)
@@ -79,7 +80,7 @@ async function resetUser (argv) {
7980
const pass = getPass(argv, 'reset')
8081

8182
// set password and save
82-
existingUser.password = pass
83+
existingUser.password = await models.User.hashPassword(pass)
8384
await existingUser.save()
8485
console.log(`User with email ${argv['reset']} password has been reset`)
8586
}

lib/auth/email/index.js

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,56 @@ const emailAuth = module.exports = Router()
1515

1616
passport.use(new LocalStrategy({
1717
usernameField: 'email'
18-
}, function (email, password, done) {
18+
}, async function (email, password, done) {
1919
if (!validator.isEmail(email)) return done(null, false)
20-
models.User.findOne({
21-
where: {
22-
email: email
23-
}
24-
}).then(function (user) {
20+
21+
try {
22+
const user = await models.User.findOne({
23+
where: {
24+
email: email
25+
}
26+
})
27+
2528
if (!user) return done(null, false)
26-
if (!user.verifyPassword(password)) return done(null, false)
29+
if (!await user.verifyPassword(password)) return done(null, false)
2730
return done(null, user)
28-
}).catch(function (err) {
31+
} catch (err) {
2932
logger.error(err)
3033
return done(err)
31-
})
34+
}
3235
}))
3336

3437
if (config.allowEmailRegister) {
35-
emailAuth.post('/register', urlencodedParser, function (req, res, next) {
38+
emailAuth.post('/register', urlencodedParser, async function (req, res, next) {
3639
if (!req.body.email || !req.body.password) return response.errorBadRequest(req, res)
3740
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(req, res)
38-
models.User.findOrCreate({
39-
where: {
40-
email: req.body.email
41-
},
42-
defaults: {
43-
password: req.body.password
44-
}
45-
}).spread(function (user, created) {
46-
if (user) {
47-
if (created) {
48-
logger.debug('user registered: ' + user.id)
49-
req.flash('info', "You've successfully registered, please signin.")
50-
} else {
51-
logger.debug('user found: ' + user.id)
52-
req.flash('error', 'This email has been used, please try another one.')
41+
try {
42+
const [user, created] = await models.User.findOrCreate({
43+
where: {
44+
email: req.body.email
45+
},
46+
defaults: {
47+
password: await models.User.hashPassword(req.body.password)
5348
}
49+
})
50+
51+
if (!user) {
52+
req.flash('error', 'Failed to register your account, please try again.')
5453
return res.redirect(config.serverURL + '/')
5554
}
56-
req.flash('error', 'Failed to register your account, please try again.')
55+
56+
if (created) {
57+
logger.debug('user registered: ' + user.id)
58+
req.flash('info', "You've successfully registered, please signin.")
59+
} else {
60+
logger.debug('user found: ' + user.id)
61+
req.flash('error', 'This email has been used, please try another one.')
62+
}
5763
return res.redirect(config.serverURL + '/')
58-
}).catch(function (err) {
64+
} catch (err) {
5965
logger.error('auth callback failed: ' + err)
6066
return response.errorInternalError(req, res)
61-
})
67+
}
6268
})
6369
}
6470

lib/models/user.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ module.exports = function (sequelize, DataTypes) {
4545
}
4646
})
4747

48-
User.prototype.hashPassword = async function (plain) {
49-
return Scrypt.kdf(plain, await Scrypt.pickParams(0.1)).toString('hex')
48+
User.hashPassword = async function (plain) {
49+
return (await Scrypt.kdf(plain, await Scrypt.pickParams(0.1))).toString('hex')
5050
}
5151

5252
User.prototype.verifyPassword = async function (attempt) {
53-
if (await Scrypt.verifyKdf(Buffer.from(this.password, 'hex'), attempt)) {
53+
if (await Scrypt.verify(Buffer.from(this.password, 'hex'), attempt)) {
5454
return this
5555
}
5656

0 commit comments

Comments
 (0)