f-23 / react-native-passkey

Passkeys for React Native
MIT License
121 stars 25 forks source link

react-native-passkey

Build Version License

Native Passkeys on iOS 15.0+ and Android API 28+ using React Native.

Please note that Android support is still in alpha. (More info)

Progress is being tracked here.

Installation

Javascript

For the javascript part of the installation you need to run

npm install react-native-passkey

or

yarn add react-native-passkey

Native

For the native part of the installation you need to run

cd ios && pod install

in the root of your React Native project.


Configuration

iOS

There are iOS specific steps you need to go through in order to configure Passkey support. If you have already set up an associated domain for your application you can skip this step.

Set up an associated domain for your application (More info)

The Android specific configuration is similar to iOS. If you have already set up Digital Asset Links for your application you can skip this step.

Associate your app with a domain (More info)


Usage

Check if Passkeys are supported

import { Passkey } from 'react-native-passkey';

// Use this method to check if passkeys are supported on the device

const isSupported: boolean = Passkey.isSupported();

Creating a new Passkey

import { Passkey, PasskeyRegistrationResult } from 'react-native-passkey';

// Retrieve a valid FIDO2 attestation request from your server
// The challenge inside the request needs to be a base64 encoded string
// There are plenty of libraries which can be used for this (e.g. fido2-lib)

try {
  // Call the `register` method with the retrieved request in JSON format
  // A native overlay will be displayed
  const result: PasskeyRegistrationResult = await Passkey.register(requestJson);

  // The `register` method returns a FIDO2 attestation result
  // Pass it to your server for verification
} catch (error) {
  // Handle Error...
}

Authenticating with existing Passkey

import { Passkey, PasskeyAuthenticationResult } from 'react-native-passkey';

// Retrieve a valid FIDO2 assertion request from your server 
// The challenge inside the request needs to be a base64 encoded string
// There are plenty of libraries which can be used for this (e.g. fido2-lib)

try {
  // Call the `authenticate` method with the retrieved request in JSON format 
  // A native overlay will be displayed
  const result: PasskeyAuthResult = await Passkey.authenticate(requestJson);

  // The `authenticate` method returns a FIDO2 assertion result
  // Pass it to your server for verification
} catch (error) {
  // Handle Error...
}

Security Keys (iOS-only)

You can allow users to register and authenticate using a Security Key (like Yubikey).

For this just pass an options object containing { withSecurityKey: true } to the Passkey.authenticate() or Passkey.register() methods.

const result: PasskeyAuthResult = await Passkey.authenticate(
  requestJson,
  { withSecurityKey: true }
);

or

const result: PasskeyRegistrationResult = await Passkey.register(
  requestJson,
  { withSecurityKey: true }
);

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.


License

MIT