function register(req, res, next) { const database = this.get('database'); database .findOne('users', { mail: req.body.email }) .then((user) => { if (user) { const error = new Error('User already exists'); error.status = 409; throw error; } const hash = bcrypt.hashSync(req.body.password, 10); const document = { email: req.body.email, password: hash, name: req.body.name, }; database .insertOne('users', document) .then(() => { res.status(200).send('User created'); }) .catch((errCreating) => next(errCreating)); }) .catch((err) => next(err)); }
set(val) { // 加密 const salt = bcrypt.genSaltSync(10); // 生成加密密码 const psw = bcrypt.hashSync(val, salt); this.setDataValue("password", psw); }
// ========================================================================= // EMAIL SIGNUP ============================================================ // ========================================================================= passport.use('email-signup', new LocalStrategy({ // by default, local strategy uses username and password, we will override with email usernameField: 'email', passwordField: 'password', passReqToCallback: true // allows us to pass back the entire request to the callback }, function (req, name, password, done) { User.findOne('email', name.toLowerCase(), function (userToUpdate) { userToUpdate.set("password", bcrypt.hashSync(password, 10)); userToUpdate.update(function (err) { Invitation.findOne("user_id", userToUpdate.get("id"), function (invite) { invite.delete(function () { return done(null, userToUpdate); }); }); }); }); }));
let reset = new ResetRequest({ user_id: user.get("id"), hash: bcrypt.hashSync(token, 10) }); reset.create(function(err, newReset){
var newUser = new User({"email": name, "password": bcrypt.hashSync(password, 10), "role_id": 1}); newUser.createWithStripe(function (err, result) { console.log(result.get('id'));
newUserData.id = oldUser.get("id"); if (newUserData.password) { newUserData.password = bcrypt.hashSync(newUserData.password, 10);
User.findById(foundInvitation.get("user_id"), function (newUser) { req.body.id = newUser.get("id"); req.body.password = bcrypt.hashSync(req.body.password, 10); Object.assign(newUser.data, req.body); newUser.set("status", "active"); newUser.set("password", bcrypt.hashSync(req.body.password, 10)); newUser.createWithStripe(function (err, result) { if (err) {
buildHashSourceFromEntity(string1, string2, hashedParam) { // don't hash only if hashedParam is false let hashed = (typeof hashedParam!=="undefined" && hashedParam===false) ? false : true; let comboString = string1.substr(12,10)+string2+string1.substr(30); if (!hashed) { return comboString; } let hashSource = bcrypt.hashSync(comboString, 10); return hashSource; }
/** * Generate a hash based on bcrypt algorithm * @param {string} plainText input text string * @return {string} hashed string */ const makeHash = (plainText) => { if (!plainText) return; return bcrypt.hashSync(plainText, bcrypt.genSaltSync(10)); }
knex('users').del() .then(() => { const salt = bcrypt.genSaltSync(); const hash = bcrypt.hashSync('johnson123', salt); return Promise.join( knex('users').insert({ username: 'jeremy', password: hash, }) // eslint-disable-line ); }) .catch((err) => { console.log(err); })
router.post('/register', async (req, res) => { const hashedPassword = bcrypt.hashSync(req.body.password) const user = new User({ email: req.body.email, password: hashedPassword, active: true }) await user.save() const token = jwt.sign({ id: user._id }, fs.readFileSync(config.jwt.secret.PRIVATE_KEY), { algorithm: 'RS256', expiresIn: config.jwt.EXPIRES_IN }) res.send({ token }) })
// 存入資料庫前把密碼 Hash 起來 UserSchema.pre('save', function (next) { let user = this if (user.isModified('password')) { user.password = bcryptjs.hashSync(user.password, 10) } next() })
function createUser(req) { const salt = bcrypt.genSaltSync(); const hash = bcrypt.hashSync(req.body.password, salt); return knex('users') .insert({ username: req.body.username, password: hash, }) .returning('*'); }
const _withHashedPassword = (user, password) => { const bcrypt = require('bcryptjs') const salt = bcrypt.genSaltSync(10) user.hashed_password = bcrypt.hashSync(password, salt) return user }
function createHashPassword(password) { const salt = bcrypt.genSaltSync() const hash = bcrypt.hashSync(password, salt); return hash; }