check if password is reused in typescript

To check if a password is reused, you need to use hashing and store the hashed value in a database. When a user enters a password, the entered password is hashed and compared to the hashed value stored in the database.

Here's an example of how to check if a password is reused using TypeScript and bcrypt.js, a popular hashing library:

index.ts
import bcrypt from 'bcrypt';

async function checkIfPasswordIsReused(password: string, hashedPassword: string): Promise<boolean> {
  const isPasswordMatch = await bcrypt.compare(password, hashedPassword);
  return isPasswordMatch;
}

// Usage
const enteredPassword = 'password123';
const hashedPasswordFromDb = '$2b$10$jMkpfE0XZYByN4.zGjKsruDOJgAcJVxiNQrGKkV7h8ZghAtu9jQ2a';
const isPasswordReused = await checkIfPasswordIsReused(enteredPassword, hashedPasswordFromDb);
console.log(isPasswordReused); // true or false
518 chars
13 lines

In the code above, checkIfPasswordIsReused is a function that takes in two parameters - the entered password and the hashed password from the database. bcrypt.compare is used to compare the entered password with the stored hashed password, returning a boolean value indicating whether the passwords match or not.

If the function returns true, then the entered password is the same as the password that was previously used and hashed.

gistlibby LogSnag