Skip to main content
The idle lock protects a signed-in session while the app is not in use. After a set time in the background, the SDK locks the session. To continue, the user enters an app passcode or, if enabled, passes Face ID, Touch ID, or a fingerprint check. The unlock works offline: no network call and no new sign-in. While the session is locked, the client is sealed:
  • The profile and the access token are hidden.
  • Every protected call and every refresh fails with a “session locked” error.
  • On unlock, the SDK restores the session from the secure store, without a network call.
Apps that do not create an idle lock see no change. Pick your platform once: tabs across this section stay in sync.

Requirements

The Flutter SDK does not have the idle lock yet.

How it works

  1. Create the lock with a threshold, the number of seconds the app can stay in the background before it locks, and start it.
  2. Set a passcode after sign-in. The lock does nothing until a passcode is set, so there is always a way back in. Show a “set a passcode” screen while no passcode is set.
  3. Optionally enable biometrics as a shortcut past the passcode.
  4. The app goes to the background. When it comes back after more than the threshold, the SDK seals the client and reports the lock as locked. Show your lock screen.
  5. The user unlocks with the passcode or with biometrics. The SDK unseals the client and the session continues.
  6. On sign-out, disable the lock. This forgets the passcode and the biometric shortcut.
By default, the lock also engages when the app starts (lockOnStart), so closing and reopening the app is not a way past it. Wrong passcodes are limited to maxAttempts (default 5). After the last one, unlocking with the passcode fails with a “locked out” error: sign the user out.
The passcode never leaves the device. On a device, the SDK protects it with the Keychain (iOS) or a key in the Android Keystore, and the biometric shortcut is an item that the system only releases after a successful biometric check.

Platform setup

For biometrics, add NSFaceIDUsageDescription to your app’s Info.plist. iOS shows this text the first time the app uses Face ID:

Create and start the lock

Create one lock per client, start it once, and keep it for the life of the client.
PreludeIdleLock is an ObservableObject. Its published properties drive your screens:Other parameters: lockOnStart (default true) and maxAttempts (default 5).

Set the passcode

Ask for a passcode right after sign-in, while isEnabled is false. The passcode needs at least 4 characters. Setting a new passcode replaces the old one and turns biometrics off, so the user enables it again.

Enable biometrics

Offer biometrics when isBiometricsAvailable is true. It needs a passcode first.

Unlock

Show your lock screen while isLocked is true. The SDK does not show a biometric prompt on its own: call unlockWithBiometrics from a button, or when the lock screen appears.
To lock the session at once, for example from a “Lock now” button, call lock(). It does nothing when no passcode is set.

Sign out

On sign-out, call disable(). It forgets the passcode and the biometric shortcut and removes the lock, so the next user of the device starts without them. With the React layer, signOut() does this for you.

React hooks

With PreludeAuthProvider, pass the lock options as the idleLock prop. The provider creates and starts the lock before it restores the session, so with lockOnStart a relaunch lands on the lock screen.
  • While the session is locked, useAuth().stage is "locked" and user is null, so SignedIn unmounts and Locked renders.
  • In Home, show a passcode setup while useIdleLock().isEnabled is false.
  • Hook actions never throw: a wrong passcode lands in useIdleLock().error. On LockedOutError the provider signs the user out.
  • Every path that ends the session (signOut, a rejected refresh, a launch with no session) forgets the passcode.

Errors

Each SDK names these errors in its own convention, for example PreludeAuthError.lockedOut (iOS), PreludeAuthError.LockedOut (Android), and LockedOutError (React Native).

Test on a simulator or emulator

  • iOS Simulator: setting a passcode needs PreludeAuth 0.8.1 or later (React Native 0.8.1 or later). For biometrics, turn on Features > Face ID > Enrolled. The simulator does not show the Face ID sheet for the biometric unlock, so test the face check itself on a device.
  • Android emulator: set a screen lock and enroll a fingerprint in Settings > Security, then touch the sensor from the emulator’s extended controls (Fingerprint), or with adb -e emu finger touch 1.