本文整理汇总了TypeScript中bcryptjs.genSalt函数的典型用法代码示例。如果您正苦于以下问题:TypeScript genSalt函数的具体用法?TypeScript genSalt怎么用?TypeScript genSalt使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了genSalt函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: function
UserSchema.pre('save', function (next: Function): void {
let user = this
// check if admin (no place needed)
if (!user.admin && !user.placeList) {
return next(new Error('palceList field needed'))
} else if (!user.admin && user.placeList.length === 0) {
return next(new Error('palceList field needed'))
}
// TODO validate placeList ids
// only hash the password if it has been modified or is new
if (user.isModified('password') || user.isNew) {
bcrypt.genSalt(SALT, (err1, salt) => {
if (err1) { return next(err1) }
bcrypt.hash(user.password, salt, (err2, hash) => {
if (err2) { return next(err2) }
user.password = hash
next()
})
})
} else {
next()
}
})
示例2: async
export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'currentPasword' parameter
const [currentPassword, currentPasswordErr] = $.str.get(params.currentPasword);
if (currentPasswordErr) return rej('invalid currentPasword param');
// Get 'newPassword' parameter
const [newPassword, newPasswordErr] = $.str.get(params.newPassword);
if (newPasswordErr) return rej('invalid newPassword param');
// Compare password
const same = await bcrypt.compare(currentPassword, user.password);
if (!same) {
return rej('incorrect password');
}
// Generate hash of password
const salt = await bcrypt.genSalt(8);
const hash = await bcrypt.hash(newPassword, salt);
await User.update(user._id, {
$set: {
'password': hash
}
});
res();
});
示例3: function
userSchema.pre('save', function (next) {
if (!this.isModified('password')) {return next(); }
bcrypt.genSalt(10, function(err, salt) {
if (err) {return next(err); }
bcrypt.hash(this.password, salt, function(error, hash) {
if (error) {return next(error); }
this.password = hash;
next();
});
});
});
示例4: function
UserSchema.pre('save', function (next) {
const user = this as IUserModel;
bcrypt.genSalt(10, (err, salt) => {
if (err) {
return next(err)
}
bcrypt.hash(user.password, salt, (err, hash) => {
if (err) {
return next(err)
}
user.password = hash;
next();
})
});
});
示例5: hash
export async function hash(password: string) {
const salt = await bcrypt.genSalt(10);
return bcrypt.hash(password, salt);
}
示例6: async
export default async (ctx: Koa.Context) => {
const body = ctx.request.body as any;
// Verify recaptcha
// ただしテスト時はこの機構は障害となるため無効にする
if (process.env.NODE_ENV !== 'test' && config.recaptcha != null) {
const success = await recaptcha(body['g-recaptcha-response']);
if (!success) {
ctx.throw(400, 'recaptcha-failed');
return;
}
}
const username = body['username'];
const password = body['password'];
// Validate username
if (!validateUsername(username)) {
ctx.status = 400;
return;
}
// Validate password
if (!validatePassword(password)) {
ctx.status = 400;
return;
}
// Fetch exist user that same username
const usernameExist = await User
.count({
usernameLower: username.toLowerCase(),
host: null
}, {
limit: 1
});
// Check username already used
if (usernameExist !== 0) {
ctx.status = 400;
return;
}
// Generate hash of password
const salt = await bcrypt.genSalt(8);
const hash = await bcrypt.hash(password, salt);
// Generate secret
const secret = generateUserToken();
// Create account
const account: IUser = await User.insert({
avatarId: null,
bannerId: null,
createdAt: new Date(),
description: null,
followersCount: 0,
followingCount: 0,
name: null,
notesCount: 0,
username: username,
usernameLower: username.toLowerCase(),
host: null,
keypair: generateKeypair(),
token: secret,
email: null,
password: hash,
profile: {
bio: null,
birthday: null,
blood: null,
gender: null,
handedness: null,
height: null,
location: null,
weight: null
},
settings: {
autoWatch: false
}
});
//#region Increment users count
Meta.update({}, {
$inc: {
'stats.usersCount': 1,
'stats.originalUsersCount': 1
}
}, { upsert: true });
//#endregion
// Response
ctx.body = await pack(account);
};