當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript bcryptjs.genSalt函數代碼示例

本文整理匯總了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()
  }
})
開發者ID:chipp972,項目名稱:stock_manager_api,代碼行數:28,代碼來源:user.ts

示例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();
});
開發者ID:ha-dai,項目名稱:Misskey,代碼行數:28,代碼來源:change_password.ts

示例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();
    });
  });
});
開發者ID:pontsoleil,項目名稱:WuWei,代碼行數:11,代碼來源:user.ts

示例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();
        })
    });
});
開發者ID:Antyneskul,項目名稱:homeBudgetApi,代碼行數:18,代碼來源:user.ts

示例5: hash

export async function hash(password: string) {
  const salt = await bcrypt.genSalt(10);
  return bcrypt.hash(password, salt);
}
開發者ID:7h1b0,項目名稱:Anna,代碼行數:4,代碼來源:cryptoUtil.ts

示例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);
};
開發者ID:ha-dai,項目名稱:Misskey,代碼行數:95,代碼來源:signup.ts


注:本文中的bcryptjs.genSalt函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。