# How to authenticate with biometrics (fingerprint / Face ID)

> Check availability and authenticate the user with fingerprint, Face ID, or Windows Hello.

Source: https://crossfly.dev/docs/cookbook/device/biometria — CrossFly documentation. Full docs as one file: https://crossfly.dev/llms-full.txt

# How to authenticate with biometrics (fingerprint / Face ID)

**Purpose.** Protect a sensitive action by requesting **biometric authentication** (fingerprint, Face ID/Touch
ID, or Windows Hello on desktop). The result arrives via callback, asynchronously.

## APIs used
- `boolean Biometry.isAvailable()` — `true` if the device has at least one biometric method available. **Always call it before** authenticating.
- `void Biometry.authenticate(String reason, String cancel, boolean onlyBiometric, Biometry.Callback callback)` — shows the native biometric UI.
  - `reason` — text explaining why authentication is needed.
  - `cancel` — label of the cancel button.
  - `onlyBiometric` — `true` = biometrics only (no fallback to PIN/password); `false` = allows PIN/password.
- `Biometry.Callback.onResult(AuthResult result, String message)` — asynchronous result.
- `Biometry.AuthResult` (enum) — `SUCCESS`, `FAILURE`, `CANCELLED`, `ERROR`.

> **Note.** The callback arrives **off the paint thread** — wrap any UI update in
> `Component.onUI(...)`. Support: Android (BiometricPrompt), iOS (Face ID/Touch ID), limited desktop
> (Windows Hello). Biometric data never leaves the device — only the result is returned.

## Complete example (copy, paste, and run)

```java
package com.exemplo;

import dev.crossfly.ui.*;                   // Window, Component, Button, Label
import dev.crossfly.ui.layout.*;            // VerticalLayout
import dev.crossfly.device.*;               // Biometry, Biometry.AuthResult

/** Runnable main screen. Point the launcher (classname) and run: gradlew :lwjgl3:run */
public class MainWindow extends Window
{
   @Override
   public void onShow() throws Exception
   {
      super.onShow();
      setLayout(new VerticalLayout());

      Label resultado = new Label("");
      add(LAY_AUTO, resultado);

      Button acao = new Button("Autenticar");
      acao.onAction(c ->
      {
         if (Biometry.isAvailable())
         {
            Biometry.authenticate(
               "Autentique-se para acessar sua conta",  // reason
               "Cancelar",                               // cancel
               false,                                    // onlyBiometric=false → allows PIN/password
               (result, message) -> Component.onUI(() ->   // callback outside the paint thread
               {
                  switch (result)
                  {
                     case SUCCESS:   resultado.setText("Acesso liberado");        break;
                     case FAILURE:   resultado.setText("Falhou: " + message);     break;
                     case CANCELLED: resultado.setText("Cancelado");              break;
                     case ERROR:     resultado.setText("Erro: " + message);       break;
                  }
               }));
         }
         else
         {
            resultado.setText("Sem biometria disponível");   // fall back to the application's password/PIN
         }
      });
      add(LAY_AUTO, acao);
   }
}
```

> The snippet below shows **only the core**. To run it, place it in the `MainWindow` skeleton above.

## Minimal snippet

```java
// verified at source: Biometry.java (isAvailable, authenticate, Callback.onResult, AuthResult)
import dev.crossfly.device.*;              // Biometry, Biometry.AuthResult
import dev.crossfly.ui.*;                  // Component (onUI)

if (Biometry.isAvailable()) {
   Biometry.authenticate(
      "Autentique-se para acessar sua conta",  // reason
      "Cancelar",                               // cancel
      false,                                    // onlyBiometric=false → allows PIN/password
      (result, message) -> Component.onUI(() -> {   // callback outside the paint thread
         switch (result) {
            case SUCCESS:   liberarAcesso();                     break;
            case FAILURE:   mostrarErro("Falhou: " + message);   break;
            case CANCELLED: /* user cancelled */               break;
            case ERROR:     mostrarErro("Erro: " + message);     break;
         }
      }));
} else {
   // No biometrics: fall back to the application's password/PIN
}
```

## Go deeper
- Biometrics guide: [Biometrics](/docs/data-and-services/device-biometry)
