1import { UnavailabilityError } from 'expo-errors';
2import invariant from 'invariant';
3import { Platform } from 'react-native';
4
5import ExpoLocalAuthentication from './ExpoLocalAuthentication';
6
7import { AuthenticationType, LocalAuthenticationResult } from './LocalAuthentication.types';
8
9export { AuthenticationType, LocalAuthenticationResult };
10
11export async function hasHardwareAsync(): Promise<boolean> {
12  if (!ExpoLocalAuthentication.hasHardwareAsync) {
13    throw new UnavailabilityError('expo-local-authentication', 'hasHardwareAsync');
14  }
15  return await ExpoLocalAuthentication.hasHardwareAsync();
16}
17
18export async function supportedAuthenticationTypesAsync(): Promise<AuthenticationType[]> {
19  if (!ExpoLocalAuthentication.supportedAuthenticationTypesAsync) {
20    throw new UnavailabilityError('expo-local-authentication', 'supportedAuthenticationTypesAsync');
21  }
22  return await ExpoLocalAuthentication.supportedAuthenticationTypesAsync();
23}
24
25export async function isEnrolledAsync(): Promise<boolean> {
26  if (!ExpoLocalAuthentication.isEnrolledAsync) {
27    throw new UnavailabilityError('expo-local-authentication', 'isEnrolledAsync');
28  }
29  return await ExpoLocalAuthentication.isEnrolledAsync();
30}
31
32export async function authenticateAsync(
33  promptMessageIOS: string = 'Authenticate'
34): Promise<LocalAuthenticationResult> {
35  if (!ExpoLocalAuthentication.authenticateAsync) {
36    throw new UnavailabilityError('expo-local-authentication', 'authenticateAsync');
37  }
38
39  if (Platform.OS === 'ios') {
40    invariant(
41      typeof promptMessageIOS === 'string' && promptMessageIOS.length,
42      'Fingerprint.authenticateAsync must be called with a non-empty string on iOS'
43    );
44
45    const result = await ExpoLocalAuthentication.authenticateAsync(promptMessageIOS);
46
47    if (result.warning) {
48      console.warn(result.warning);
49    }
50    return result;
51  } else {
52    return await ExpoLocalAuthentication.authenticateAsync();
53  }
54}
55
56export async function cancelAuthenticate(): Promise<void> {
57  if (!ExpoLocalAuthentication.cancelAuthenticate) {
58    throw new UnavailabilityError('expo-local-authentication', 'cancelAuthenticate');
59  }
60  await ExpoLocalAuthentication.cancelAuthenticate();
61}
62