本文整理汇总了TypeScript中bcryptjs.hash函数的典型用法代码示例。如果您正苦于以下问题:TypeScript hash函数的具体用法?TypeScript hash怎么用?TypeScript hash使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了hash函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: initTestData
export async function initTestData() {
if (isProd()) {
throw new Error("How about you stop calling initTestData in production!")
}
await User.insertMany([
{
name: "John Smith",
email: "john.smith@example.com",
logins: [{
id: "john.smith@example.com",
type: "local",
primary: true,
password: await bcrypt.hash("johnsmith123", 10),
}],
},
{
name: "John Doe",
email: "john.doe@example.com",
logins: [{
id: "john.doe@example.com",
type: "local",
primary: true,
password: await bcrypt.hash("johndoe123", 10),
}],
},
])
}
示例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
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: async
UserSchema.statics.registerUser = async (user: RegisterRequest): Promise<IUserDocument | undefined> => {
const doc = new User();
doc.name = user.name
doc.email = user.email
const password = await bcrypt.hash(user.password, PASSWORD_SALT_ROUNDS)
const newLogin = new LoginId(user.email, LoginType.Local, password)
doc.logins.push(newLogin);
return await doc.save();
}
示例5: Promise
return new Promise((resolve: Function, reject: Function) => {
bcrypt.hash(password, config.security.bcryptRounds, function(err, passwordHash) {
if (err) {
log.error("Failed to generate password hash", JSON.stringify(err, null, ' '));
reject(new Error(Errors.PASSWORD_ALGORITHM_FAILURE));
return;
}
resolve(encrypt(passwordHash));
});
});
示例6: next
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()
})
})
示例7: function
const seedInitialData = function(){
if (!instance['app_user'] || !instance['app_user'].findOneSync({})){
var defaultUser = new AppUser("test@ic.le");
defaultUser.registrationDate = moment.utc().toISOString();
bcrypt.hash('password', 10, function(err, hash){
defaultUser.passwordHash = hash;
instance.saveDoc('app_user', defaultUser, function(err, res){
console.log(`User ${res.id} - '${res.email}' is seeded`);
});
});
}
};
示例8: next
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();
})
});
示例9: async
const signIn = async (input: NexusGenAllTypes['SignInInput']) => {
const fail = (errors: NexusGenAllTypes['SignInErrors']) => ({
errors: {
email: null,
password: null,
...errors,
},
token: null,
});
const success = (userId: string) => {
const payload: JsonWebTokenPayload = { userId };
return {
errors: null,
token: jwt.sign(payload, API_SECRET as jwt.Secret),
};
};
const errors = validateSignIn(input);
if (context.input.hasError(errors)) return fail(errors);
if (input.createAccount) {
const exists = await context.prisma.$exists.user({ email: input.email });
if (exists) return fail({ email: 'ALREADY_EXISTS' });
const password = await bcrypt.hash(input.password, 10);
const user = await context.prisma.createUser({
email: input.email,
password,
themeName: '',
});
return success(user.id);
}
const user = await context.prisma.user({ email: input.email });
if (!user) return fail({ email: 'NOT_EXISTS' });
const validPassword = await bcrypt.compare(input.password, user.password);
if (!validPassword) return fail({ password: 'WRONG_PASSWORD' });
return success(user.id);
};
示例10: async
export default async (event: FunctionEvent<EventData>) => {
console.log(event);
try {
const graphcool = fromEvent(event);
const api = graphcool.api('simple/v1');
const { email, password } = event.data;
if (!validator.isEmail(email)) {
return { error: 'Not a valid email' };
}
// check if user exists already
const userExists: boolean = await getUser(api, email).then(
r => r.User !== null,
);
if (userExists) {
return { error: 'Email already in use' };
}
// create password hash
const salt = bcrypt.genSaltSync(SALT_ROUNDS);
const hash = await bcrypt.hash(password, SALT_ROUNDS);
// create new user
const userId = await createGraphcoolUser(api, email, hash);
// generate node token for new User node
const token = await graphcool.generateNodeToken(userId, 'User');
return { data: { id: userId, token } };
} catch (e) {
console.log(e);
return { error: 'An unexpected error occured during signup.' };
}
};