Skip to content

Commit

Permalink
fix: strong password type (#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
spaenleh authored Nov 12, 2024
1 parent 080b606 commit 04e0759
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/member/password.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { isStrongPassword } from '@/validation/isPasswordStrong.js';

export const isPasswordStrong = (password: string) =>
isStrongPassword(password, {
export const isPasswordStrong = (password: string): boolean => {
return isStrongPassword(password, {
minLength: 8,
minLowercase: 1,
minUppercase: 1,
minNumbers: 1,
minSymbols: 0,
});
};
25 changes: 21 additions & 4 deletions src/validation/isPasswordStrong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const defaultOptions = {
minUppercase: 1,
minNumbers: 1,
minSymbols: 1,
returnScore: false,
pointsPerUnique: 1,
pointsPerRepeat: 0.5,
pointsForContainingLower: 10,
Expand Down Expand Up @@ -83,15 +82,33 @@ function scorePassword(
return points;
}

/**
* Score the password based on the requirements
* @param str input password string
* @param options define password requirements that need to be met
* @returns the password score
*/
export function getPasswordScore(
str: string,
options: Partial<PasswordOptions>,
): number {
const analysis = analyzePassword(str);
const newOptions = merge(options || {}, defaultOptions);
return scorePassword(analysis, newOptions);
}

/**
* Validate if password follows the requirements
* @param str input password string
* @param options defines requirements to be met by the password
* @returns whether the password is strong according to the requirements
*/
export function isStrongPassword(
str: string,
options: Partial<PasswordOptions>,
) {
const analysis = analyzePassword(str);
const newOptions = merge(options || {}, defaultOptions);
if (newOptions.returnScore) {
return scorePassword(analysis, newOptions);
}
return (
analysis.length >= newOptions.minLength &&
analysis.lowercaseCount >= newOptions.minLowercase &&
Expand Down

0 comments on commit 04e0759

Please sign in to comment.