bcrypt#compare TypeScript Examples

The following examples show how to use bcrypt#compare. 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: password.service.ts    From amplication with Apache License 2.0 6 votes vote down vote up
/**
   *
   * @param password the password to be encrypted.
   * @param encrypted the encrypted password to be compared against.
   * @returns whether the password match the encrypted password
   */
  compare(password: string, encrypted: string): Promise<boolean> {
    return compare(password, encrypted);
  }
Example #2
Source File: user.service.ts    From nestjs-starter-rest-api with MIT License 6 votes vote down vote up
async validateUsernamePassword(
    ctx: RequestContext,
    username: string,
    pass: string,
  ): Promise<UserOutput> {
    this.logger.log(ctx, `${this.validateUsernamePassword.name} was called`);

    this.logger.log(ctx, `calling ${UserRepository.name}.findOne`);
    const user = await this.repository.findOne({ username });
    if (!user) throw new UnauthorizedException();

    const match = await compare(pass, user.password);
    if (!match) throw new UnauthorizedException();

    return plainToClass(UserOutput, user, {
      excludeExtraneousValues: true,
    });
  }
Example #3
Source File: utils.ts    From MyAPI with MIT License 6 votes vote down vote up
comparePassword = async (password: string, hash: string) : Promise<boolean> => {
  return compare(password, hash)
}
Example #4
Source File: changePassword.ts    From mayoor with MIT License 6 votes vote down vote up
ChangePassword = mutationField("changePassword", {
  type: "User",
  args: {
    oldPassword: stringArg({ nullable: false }),
    newPassword: stringArg({ nullable: false }),
  },
  resolve: async (_, { oldPassword, newPassword }, ctx) => {
    const { id } = await ctx.user.getCurrentUser();
    const user = await ctx.prisma.user.findOne({ where: { id } });

    if (!user) {
      throw new Error(`User with id "${id}" doesnt exist.`);
    }

    const isPasswordValid = await compare(oldPassword, user.password);

    if (!isPasswordValid) {
      throw new ApolloError("Incorrect old password", "INVALID_PASSWORD");
    }

    if (!user.canBeDeleted) {
      throw new ApolloError(
        "You cant change this users password.",
        "INVALID_OPERATION"
      );
    }

    const newHashedPwd = await hash(newPassword, 10);

    const updatedUser = ctx.prisma.user.update({
      where: { id },
      data: { password: newHashedPwd },
    });

    return updatedUser;
  },
})
Example #5
Source File: login.ts    From mayoor with MIT License 6 votes vote down vote up
Login = mutationField("login", {
  type: "AuthPayload",
  args: {
    email: stringArg({ nullable: false }),
    password: stringArg({ nullable: false }),
  },
  resolve: async (_, { email, password }, ctx) => {
    const user = await ctx.prisma.user.findOne({ where: { email } });

    if (!user || user.deleted) {
      throw new ApolloError("User not found", "USER_NOT_FOUND");
    }

    const isPasswordValid = await compare(password, user.password);

    if (!isPasswordValid) {
      throw new ApolloError("Incorrect password", "INVALID_PASSWORD");
    }

    const token = issueToken({ email: user.email, id: user.id });
    return { user, token };
  },
})
Example #6
Source File: credentialValidator.ts    From office-hours with GNU General Public License v3.0 6 votes vote down vote up
adminCredentialValidator = {
  inject: [],
  useFactory: () => {
    return async function validateCredentials(
      username: string,
      password: string,
    ): Promise<AdminUserModel | null> {
      const user = await AdminUserModel.findOne({ username });
      if (user) {
        if (await compare(password, user.passwordHash)) {
          return user;
        }
      }
      return null;
    };
  },
}
Example #7
Source File: crypt.service.ts    From wise-old-man with MIT License 6 votes vote down vote up
/**
 * Checks if a given hash matches a given code.
 */
async function verifyCode(hash: string, code: string): Promise<boolean> {
  const verified = await new Promise((resolve, reject) => {
    compare(code, hash, (err, result) => {
      if (err) reject(err);
      resolve(result);
    });
  });

  return !!verified;
}
Example #8
Source File: password.service.ts    From svvs with MIT License 5 votes vote down vote up
/**
   * Compare plain password with password hash
   * @param password Plain password
   * @param passwordHash Password hash
   */
  compareHash(password: string, passwordHash: string): Promise<boolean> {
    return compare(password, passwordHash)
  }
Example #9
Source File: password.service.ts    From amplication with Apache License 2.0 5 votes vote down vote up
validatePassword(password: string, hashedPassword: string): Promise<boolean> {
    return compare(password, hashedPassword);
  }
Example #10
Source File: user.model.ts    From nestjs-rest-sample with GNU General Public License v3.0 5 votes vote down vote up
function comparePasswordMethod(password: string): Observable<boolean> {
  return from(compare(password, this.password));
}
Example #11
Source File: auth.ts    From monkeytype with GNU General Public License v3.0 5 votes vote down vote up
async function authenticateWithApeKey(
  key: string,
  configuration: MonkeyTypes.Configuration,
  options: RequestAuthenticationOptions
): Promise<MonkeyTypes.DecodedToken> {
  if (!configuration.apeKeys.acceptKeys) {
    throw new MonkeyError(403, "ApeKeys are not being accepted at this time");
  }

  if (!options.acceptApeKeys) {
    throw new MonkeyError(401, "This endpoint does not accept ApeKeys");
  }

  try {
    const decodedKey = base64UrlDecode(key);
    const [keyId, apeKey] = decodedKey.split(".");

    const targetApeKey = await getApeKey(keyId);
    if (!targetApeKey) {
      throw new MonkeyError(404, "ApeKey not found");
    }

    if (!targetApeKey.enabled) {
      const { code, message } = statuses.APE_KEY_INACTIVE;
      throw new MonkeyError(code, message);
    }

    const isKeyValid = await compare(apeKey, targetApeKey.hash);
    if (!isKeyValid) {
      const { code, message } = statuses.APE_KEY_INVALID;
      throw new MonkeyError(code, message);
    }

    await updateLastUsedOn(targetApeKey.uid, keyId);

    return {
      type: "ApeKey",
      uid: targetApeKey.uid,
      email: "",
    };
  } catch (error) {
    if (!(error instanceof MonkeyError)) {
      const { code, message } = statuses.APE_KEY_MALFORMED;
      throw new MonkeyError(code, message);
    }

    throw error;
  }
}