Sha256: 2e2f694e29a45f898e60f37b6f47aa72da8c143994557ad26233d8aada9a7775

Contents?: true

Size: 1.8 KB

Versions: 148

Compression:

Stored size: 1.8 KB

Contents

import { useEffect, useMemo, useState } from 'react'
import zxcvbn from 'zxcvbn'

export default function useZxcvbn(options) {
  const { passphrase = '', common, isPwned, confirmation, averageThreshold, minLength, strongThreshold } = options
  const calculator = useMemo(
    () => confirmation ?  () => ({ score: 0 }) : zxcvbn,
    [confirmation]
  )

  const [percent, setPercent] = useState('0')
  const [variant, setVariant] = useState('negative')
  const [text, setText] = useState('\u00A0') //nbsp to keep height constant
  const [result, setResult] = useState({})

  useEffect(() => {
    if (confirmation) return
    const newResult = calculator(passphrase)
    setResult(newResult)
    const str = newResult.score

    const noPassphrase = passphrase.length <= 0
    const commonPassphrase = common || isPwned
    const weakPassphrase = passphrase.length < minLength || str < averageThreshold
    const averagePassphrase = str < strongThreshold
    const strongPassphrase = str >= strongThreshold

    if (noPassphrase) {
      setPercent('0')
      setVariant('negative')
      setText('\u00A0') //nbsp to keep height constant
    } else if (commonPassphrase) {
      setPercent('25')
      setVariant('negative')
      setText('This passphrase is too common')
    } else if (weakPassphrase) {
      setPercent('25')
      setVariant('negative')
      setText('Too weak')
    } else if (averagePassphrase){
      setPercent('50')
      setVariant('warning')
      setText('Almost there, keep going!')
    } else if (strongPassphrase) {
      setPercent('100')
      setVariant('positive')
      setText('Success! Strong passphrase')
    }
  }, [passphrase, common, isPwned, averageThreshold, minLength, strongThreshold]
  )

  return {
    strength: common || isPwned ? 0 : result.score,
    percent,
    variant,
    text,
  }
}

Version data entries

148 entries across 148 versions & 1 rubygems

Version Path
playbook_ui-11.13.0.pre.alpha.fontawesome1 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.13.0 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.12.1.pre.alpha.charts1 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.12.1 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.13.0.pre.alpha.fileupload1 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.12.0 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.11.0.pre.alpha.paginate2 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.11.0.pre.alpha.dialog2 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.11.0.pre.alpha.paginate1 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.11.0.pre.alpha.renderer1 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.11.0.pre.alpha.dialog1 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.11.0 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.10.0.pre.alpha.pre.bold1 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.10.0.pre.alpha.pagination1 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.10.0 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.9.0 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.9.0.pre.alpha.fileupload1 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.8.1 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.8.0 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js
playbook_ui-11.7.0.pre.alpha.table1 app/pb_kits/playbook/pb_passphrase/useZxcvbn.js