Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
Hash pw in POST /admins
Browse files Browse the repository at this point in the history
Mongoose findOneAndUpdate bypasses hooks
Automattic/mongoose#964
  • Loading branch information
jhhayashi committed Dec 20, 2016
1 parent 3722089 commit a2652f5
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions controllers/admins.js
@@ -1,4 +1,5 @@
const User = require('../models/schemas/user');
const bcrypt = require('bcrypt-nodejs');

exports.createAdmin = (req, res, next) => {
if (typeof req.body.email !== 'string')
Expand Down Expand Up @@ -53,6 +54,10 @@ exports.createAdmin = (req, res, next) => {
if (req.body.hash)
userData.hash = req.body.hash;

// hash pw, since mongoose findOneAndUpdate bypasses hooks
// https://github.com/Automattic/mongoose/issues/964
userData.hash = bcrypt.hashSync(userData.hash);

if (userData.phone)
var userQuery = {$or: [{email: userData.email}, {phone: userData.phone}]};
else
Expand Down

0 comments on commit a2652f5

Please sign in to comment.