Skip to main content

Quick start

After installation and the platform declarations, check that biometrics are available, then open the prompt:

import { LocalAuthentication } from 'nitro-local-auth'

if (LocalAuthentication.hasHardware() && LocalAuthentication.isEnrolled()) {
const result = await LocalAuthentication.authenticate({
promptMessage: 'Unlock your account',
cancelLabel: 'Not now',
})
if (result.success) {
// Unlocked.
} else if (result.error === 'user_cancel') {
// The user dismissed the prompt.
}
}

What happens:

  1. hasHardware and isEnrolled are synchronous. They read the platform state on each call.
  2. authenticate shows the system prompt. By default the user can fall back to the device passcode, PIN, pattern or password.
  3. The promise always resolves. On failure, success is false and error holds one of the error codes.

In a component​

import { useState } from 'react'
import { LocalAuthentication } from 'nitro-local-auth'

function useUnlock() {
const [unlocked, setUnlocked] = useState(false)
const unlock = async () => {
const result = await LocalAuthentication.authenticate({ promptMessage: 'Unlock' })
setUnlocked(result.success)
return result
}
return { unlocked, unlock }
}

Next​