Skip to content

How to add a hook for password under user.ts #2615

Answered by DanRibbens
clhome asked this question in Q&A
Discussion options

You must be logged in to vote

@clhome
Here is a code example for you:

/**
 * Throws error if password strength is not met. Password must have:
 *  - 8 or more characters
 *  - uppercase and lowercase letters
 *  - at least one symbol
**/
const validatePassword: CollectionBeforeValidateHook = ({ data: { password } }) => {
  let message: string;
  if (password.length <= 8) message = 'Password must be at least 8 characters long';

  const hasUpperCase = /[A-Z]/.test(password);
  const hasLowerCase = /[a-z]/.test(password);
  if (!hasUpperCase || !hasLowerCase) message = 'Password must have both uppercase and lowercase letters')

  const hasSymbols = /[$-/:-?{-~!"^_`\[\]]/.test(password);
  if (!hasSymbols) message = 'Pas…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@clhome
Comment options

Comment options

You must be logged in to vote
3 replies
@clhome
Comment options

@gycsabesz
Comment options

@berthemoose
Comment options

Answer selected by clhome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants