bcrypt#compareSync TypeScript Examples

The following examples show how to use bcrypt#compareSync. 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: auth.service.ts    From nestjs-starter with MIT License 6 votes vote down vote up
async validateUser(userData: string, password: string): Promise<User> {
    // Verify if userData is email or username
    const data: { username?: string; email?: string } = {};
    !PATTERN_VALID_EMAIL.test(userData) ? (data.username = userData) : (data.email = userData);

    const user = await this.usersService.getByUser(data);
    if (!user) {
      throw new NotFoundException('Your account does not exist');
    }
    if (!user.enabled) {
      throw new ForbiddenException('Account is disabled, contact with administrator');
    }
    const isMatch = compareSync(password, user.password);
    if (!isMatch) {
      throw new BadRequestException('Invalid credentials');
    }
    delete user.password;
    return user;
  }