By •10 min read

How to Add Passkey Login to a Flutter App with FastAPI

FlutterFastAPIPasskeysAuthenticationMobile Security
Mobile passkey sign-in connected securely to a backend storing a public key

Introduction

A passkey lets a person sign in using a credential held by their device or password manager. The phone may ask for Face ID, a fingerprint, or a device PIN, but the important difference from a local biometric unlock is that the server verifies a cryptographic response. The private key stays with the credential provider; your backend keeps the public key.

This guide is for Flutter and FastAPI teams that already have accounts and can control an HTTPS domain. You need native Android and iOS project access, an authentication database, and a secure way to identify an existing user before they add a passkey. The server examples use the public API of the Python webauthn library, checked against its examples in October 2026. This portfolio is not a runnable passkey app, so the native bridge and persistence details still require integration and device tests before production use.

My Flutter biometric authentication guide covers unlocking a locally stored session. A passkey is a different login method: it can establish a new server session. For API basics, see the FastAPI beginner guide.

Understand the Two Ceremonies

Registration and sign-in both begin on your server with a fresh challenge. The Flutter app relays options to the operating system's passkey UI, then returns the result to FastAPI for verification.

StepRegister a passkeySign in with a passkey
Prove account ownershipExisting session or verified recovery flowUsually identify the account or use discoverable credentials
Server startsGenerate registration options and challengeGenerate authentication options and challenge
Device doesCreate a new credentialSign the challenge with an existing credential
Server verifiesChallenge, origin, RP ID, user verificationChallenge, origin, RP ID, signature, user verification
Server stores or returnsCredential ID, public key, counter, ownerNew authenticated session

Do not accept "biometric success" from Flutter as proof of who owns a backend account. Android's passkey creation guide and Apple's passkey documentation both place challenge generation and credential verification on the server.

Choose One Relying-Party Domain

The relying-party ID (RP ID) is a domain you control, such as accounts.example.com. Use the same intended RP ID across your website and mobile clients. Host the required associations there:

  • Android: Digital Asset Links connect the domain to the Android package and signing certificate. The origin your server verifies includes the app's signing-certificate hash; test the Play App Signing certificate, not only a debug key. Android setup.
  • iOS: Add the webcredentials associated-domain entitlement and the site's association file for that RP ID. Without it, native passkey registration or assertion fails. Apple setup.
  • Server: Configure expected RP ID and a fixed allow-list of your actual website and app origins. Do not take an "expected origin" from the request body.

Do this before building UI. Domain mismatches are one of the easiest ways to make a perfectly reasonable Flutter screen fail on a real device.

Keep the Flutter Layer Thin

Android uses Credential Manager and CreatePublicKeyCredentialRequest or GetPublicKeyCredentialOption. iOS uses Authentication Services with ASAuthorizationPlatformPublicKeyCredentialProvider. The platform-specific code should own those requests and translate the resulting credential to JSON with binary fields encoded as base64url.

Flutter can orchestrate the network round trips without implementing WebAuthn cryptography:

Flutter asks FastAPI for registration options
  -> Flutter passes options JSON to Android or iOS
  -> OS returns a public-key credential JSON object
  -> Flutter posts the object to FastAPI
  -> FastAPI verifies and stores the credential

Use one narrow channel for createPasskey(optionsJson) and getPasskey(optionsJson), or a well-maintained Flutter package whose native implementation you have reviewed. The channel's contract should return credential JSON or a typed cancellation/error state. A user cancelling the system sheet is not an authentication failure to punish.

Flutter's platform-channel guide documents this native boundary. The Flutter/Kotlin bridge article shows how to structure Android host calls. Follow the current Android sign-in sequence and Apple's Authentication Services examples for the native side.

Let FastAPI Create and Verify Challenges

Use a reviewed WebAuthn server library instead of parsing authenticator data or verifying signatures yourself. The Python py_webauthn project exposes option generators and verification functions that can sit behind FastAPI routes.

This small registration core shows the data you must persist between start and finish. save_pending_challenge and consume_pending_challenge represent your own durable, expiring store; they are not library functions:

from webauthn import (
    generate_registration_options,
    options_to_json,
    verify_registration_response,
)

RP_ID = "accounts.example.com"
RP_NAME = "Example App"
ALLOWED_ORIGINS = [
    "https://accounts.example.com",
    "android:apk-key-hash:YOUR_RELEASE_APP_HASH",
]

def begin_registration(user):
    options = generate_registration_options(
        rp_id=RP_ID,
        rp_name=RP_NAME,
        user_id=user.stable_webauthn_id,  # bytes, not an email address
        user_name=user.email,
    )
    save_pending_challenge(
        user_id=user.id,
        kind="register",
        challenge=options.challenge,
        ttl_seconds=300,
    )
    return options_to_json(options)

def finish_registration(user, credential_json):
    challenge = consume_pending_challenge(user.id, kind="register")
    verified = verify_registration_response(
        credential=credential_json,
        expected_challenge=challenge,
        expected_rp_id=RP_ID,
        expected_origin=ALLOWED_ORIGINS,
        require_user_verification=True,
    )
    store_credential(
        user_id=user.id,
        credential_id=verified.credential_id,
        public_key=verified.credential_public_key,
        sign_count=verified.sign_count,
    )

These functions are an integration pattern, not a drop-in authentication service. In particular, the registration endpoints must require a valid existing account session or another verified account-creation flow. The challenge store must expire challenges, consume each once, and work across API instances. Credential IDs must be unique across accounts. Use database transactions so a retry cannot attach one credential to two users.

For sign-in, generate_authentication_options() returns a fresh challenge. After the phone responds, load the credential record by ID and call verify_authentication_response() with the saved challenge, RP ID, expected origin, public key, stored sign count, and user-verification requirement. Only after verification do you create your normal server session. The library's authentication example demonstrates these arguments.

The credential provider may sync a passkey across devices, so do not invent your own "counter must always increase" rule. Persist and evaluate the library's verified counter and backup flags using its guidance and your threat model.

Design Recovery Before Removing Passwords

A passkey can be lost when a person changes devices, loses access to their credential provider, or deletes the credential. Before calling your app "passwordless," define how an authenticated user adds a second passkey and how a locked-out user proves account ownership.

SituationSafe product response
User cancels the passkey sheetReturn to the sign-in screen without changing the account
New device has no passkeyOffer another approved sign-in or recovery route
User wants to remove a passkeyRequire a fresh authenticated session and preserve a recovery route
Registration response is replayedReject its already-consumed challenge
Credential belongs to another accountReject it even if its signature is valid
App signing key or domain changesRecheck associations and the server origin allow-list before release

Do not treat an emailed link, SMS, or support message as automatically strong enough for every account. Choose recovery strength based on the data and actions your app protects.

Test the Full Flow on Both Platforms

Automated API tests can cover challenge expiry, single use, origin rejection, unknown credential IDs, account separation, and session creation only after verification. Device tests should cover Android release signing, iOS associated domains, first registration, repeat sign-in, cancellation, multiple passkeys, a restored phone, and recovery. Test website login too if the same RP ID serves web users.

Use separate development and production RP domains and origin allow-lists. Record the exact library and OS versions in the test report, because passkey APIs and credential-provider behavior evolve. If you also offer password or social login, confirm the user cannot attach a passkey to a different person's account through a confused session.

Conclusion

Passkeys can simplify sign-in, but the trustworthy part is the end-to-end ceremony: controlled domain association, fresh server challenges, native credential handling, verified public-key responses, and a recovery path. Keep Flutter as the coordinator and let a reviewed WebAuthn library handle the cryptographic checks.

Adding secure sign-in to a mobile product? I help teams connect Flutter interfaces to sound backend authentication and test the real device and recovery paths. Book a meeting to review your implementation.

Share

See the project work

Explore the decisions, scope, and validation behind selected mobile and AI projects.

Explore case studies