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:
hasHardwareandisEnrolledare synchronous. They read the platform state on each call.authenticateshows the system prompt. By default the user can fall back to the device passcode, PIN, pattern or password.- The promise always resolves. On failure,
successisfalseanderrorholds 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
- Checking availability before showing an unlock option.
- Authenticating with biometrics only, custom labels and Android classes.
- Handling errors by code.