bcryptjs#genSalt TypeScript Examples

The following examples show how to use bcryptjs#genSalt. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: app.controller.ts    From NestJs-youtube with MIT License 6 votes vote down vote up
@Post('auth/register')
  async register(@Body() body: Partial<UserEntity>) {
    try {
      const salt = await genSalt(10);
      const { password, ...reset } = body;
      const u: Partial<UserEntity> = {
        salt,
        ...reset,
        password: hashSync(password, salt),
        role: Roles.user,
      };
      const user = await this.authService.register(u);
      const logedInUser = await this.authService.login(user);
      delete logedInUser.password;
      delete logedInUser.salt;
      return logedInUser;
    } catch (error) {
      throw error;
    }
  }
Example #2
Source File: seed.class.ts    From NestJs-youtube with MIT License 6 votes vote down vote up
async fakeIt<T>(entity: any): Promise<void> {
    this.salt = await genSalt(10);
    switch (entity) {
      case UserEntity:
        return this.addData(
          this.userData(),
          entity,
          (savedData: Array<Partial<UserEntity>>) => (this.users = savedData),
        );
      case PostEntity:
        return this.addData(
          this.postData(),
          entity,
          (savedData: Array<Partial<PostEntity>>) => (this.posts = savedData),
        );
      case CommentEntity:
        return this.addData(this.commentData(), entity);
      case LikeEntity:
        return this.addData(this.likeData(), entity);
      case UserFollower:
        return this.addData(this.followesData(), entity);
      default:
        break;
    }
  }
Example #3
Source File: register.ts    From TypeScript-Login-Register with MIT License 5 votes vote down vote up
alt.onClient(
  "client::lr:registerAccount",
  async (player: alt.Player, data: IRegisterAccountData) => {
    try {
      data.socialId = player.socialId;
      const loginConnection = getConnection("lr");
      const result = await loginConnection
        .createQueryBuilder(Account, "account")
        .leftJoinAndSelect("account.validation", "validation")
        .where("account.username = :username", { username: data.username })
        .orWhere("validation.socialId = :socialId", {
          socialId: data.socialId,
        })
        .orWhere("validation.scNickname = :scNickname", {
          scNickname: data.socialClub,
        })
        .orWhere("validation.licenseHash = :licenseHash", {
          licenseHash: data.licenseHash,
        })
        .getOne();
      if (result) {
        let message: string;
        if (result.username == data.username)
          message = "The given username already exists";
        else if (result.validation.scNickname == data.socialClub)
          message =
            "There is already an account linked to your socialclub name";
        else if (result.validation.socialId == data.socialId)
          message = "There is already an account linked to your socialclub id";

        let err: IErrorMessage = {
          location: "register",
          param: "username",
          msg: message,
        };
        return alt.emitClient(player, "server::lr:showRegistrationError", err);
      }

      let accValidation = new AccountValidation();
      accValidation.discordUserID = data.discordUserID;
      accValidation.licenseHash = data.licenseHash;
      accValidation.scNickname = data.socialClub;
      accValidation.socialId = data.socialId;

      let accSaveResult = await loginConnection.manager.save(accValidation);

      const salt = await genSalt(10);
      const password = await hash(data.password, salt);

      let accData = new Account();
      accData.username = data.username;
      accData.password = password;
      accData.validation = accSaveResult;

      await loginConnection.manager.save(accData);

      const loginData: ILoginAccountData = {
        socialClub: accData.validation.scNickname,
        username: accData.username,
        discordUserID: accData.validation.discordUserID,
      };

      player.setSyncedMeta("userData", loginData);

      alt.emitClient(player, "client:lr:registrationSuccessfull");
    } catch (error) {
      alt.log(error);
      let err: IErrorMessage = {
        location: "server",
        param: "",
        msg: "Internal server error, please try again later",
      };
      return alt.emitClient(player, "server::lr:showRegistrationError", err);
    }
  }
);
Example #4
Source File: User.ts    From typescript-clean-architecture with MIT License 5 votes vote down vote up
public async hashPassword(): Promise<void> {
    const salt: string = await genSalt();
    this.password = await hash(this.password, salt);
    
    await this.validate();
  }