|
1 | 1 | import { StatusBar } from 'expo-status-bar';
|
2 |
| -import React from 'react'; |
3 |
| -import { StyleSheet, Text, View } from 'react-native'; |
| 2 | +import React, { useState, useCallback } from 'react'; |
| 3 | +import { |
| 4 | + StyleSheet, |
| 5 | + Text, |
| 6 | + TextInput, |
| 7 | + View, |
| 8 | + ScrollView, |
| 9 | + SafeAreaView, |
| 10 | + RefreshControl, |
| 11 | + Button, |
| 12 | + Image, |
| 13 | + TouchableOpacity, |
| 14 | + TouchableHighlight, |
| 15 | + Alert, |
| 16 | + BackHandler, |
| 17 | + Platform, |
| 18 | + Dimensions, |
| 19 | +} from 'react-native'; |
| 20 | +import { useDeviceOrientation } from '@react-native-community/hooks'; |
| 21 | +import * as LocalAuthentication from 'expo-local-authentication'; |
| 22 | + |
| 23 | +const wait = (timeout) => { |
| 24 | + return new Promise((resolve) => setTimeout(resolve, timeout)); |
| 25 | +}; |
4 | 26 |
|
5 | 27 | export default function App() {
|
| 28 | + const [refreshing, setRefreshing] = useState(false); |
| 29 | + const { portrait, landscape } = useDeviceOrientation(); |
| 30 | + |
| 31 | + const onRefresh = useCallback(() => { |
| 32 | + setRefreshing(true); |
| 33 | + wait(2000).then(() => setRefreshing(false)); |
| 34 | + }, []); |
| 35 | + |
| 36 | + const onPressLearnMore = () => { |
| 37 | + alert('Learn More Pressed', Dimensions.get('screen')); |
| 38 | + }; |
| 39 | + |
| 40 | + const fallBackToDefaultAuth = () => { |
| 41 | + console.log('fall back to password authentication'); |
| 42 | + }; |
| 43 | + |
| 44 | + const alertComponent = (title, mess, btnTxt, btnFunc) => { |
| 45 | + return Alert.alert(title, mess, [ |
| 46 | + { |
| 47 | + text: btnTxt, |
| 48 | + onPress: btnFunc, |
| 49 | + }, |
| 50 | + ]); |
| 51 | + }; |
| 52 | + |
| 53 | + const handleBiometricAuth = async () => { |
| 54 | + // Check if hardware supports biometrics |
| 55 | + const isBiometricAvailable = await LocalAuthentication.hasHardwareAsync(); |
| 56 | + |
| 57 | + // Fallback to default authentication method (password) if Fingerprint is not available |
| 58 | + if (!isBiometricAvailable) |
| 59 | + return alertComponent( |
| 60 | + 'Please enter your password', |
| 61 | + 'Biometric Authentication not supported', |
| 62 | + 'OK', |
| 63 | + () => fallBackToDefaultAuth() |
| 64 | + ); |
| 65 | + |
| 66 | + // Check Biometrics types available (Fingerprint, Facial recognition, Iris recognition) |
| 67 | + let supportedBiometrics; |
| 68 | + if (isBiometricAvailable) |
| 69 | + supportedBiometrics = await LocalAuthentication.supportedAuthenticationTypesAsync(); |
| 70 | + |
| 71 | + // Check Biometrics are saved locally in user's device |
| 72 | + const savedBiometrics = await LocalAuthentication.isEnrolledAsync(); |
| 73 | + if (!savedBiometrics) |
| 74 | + return alertComponent( |
| 75 | + 'Biometric not registered', |
| 76 | + 'Please login with your password', |
| 77 | + 'OK', |
| 78 | + () => fallBackToDefaultAuth() |
| 79 | + ); |
| 80 | + |
| 81 | + // promptMessage?: string; |
| 82 | + // cancelLabel?: string; |
| 83 | + // disableDeviceFallback?: boolean; |
| 84 | + // fallbackLabel ?: string; |
| 85 | + |
| 86 | + // Authenticate use with Biometrics (Fingerprint, Facial recognition, Iris recognition) |
| 87 | + const biometricPrompt = await LocalAuthentication.authenticateAsync( |
| 88 | + 'Touch the screen to' |
| 89 | + ); |
| 90 | + |
| 91 | + console.log({ isBiometricAvailable }); |
| 92 | + console.log({ supportedBiometrics }); |
| 93 | + console.log({ savedBiometrics }); |
| 94 | + console.log({ biometricPrompt }); |
| 95 | + }; |
| 96 | + |
6 | 97 | return (
|
7 |
| - <View style={styles.container}> |
8 |
| - <Text>Open up App.js to start working on your app!</Text> |
9 |
| - <StatusBar style="auto" /> |
10 |
| - </View> |
| 98 | + <SafeAreaView style={styles.container}> |
| 99 | + <ScrollView |
| 100 | + contentContainerStyle={styles.scrollView} |
| 101 | + refreshControl={ |
| 102 | + <RefreshControl refreshing={refreshing} onRefresh={onRefresh} /> |
| 103 | + } |
| 104 | + > |
| 105 | + <View style={styles.container}> |
| 106 | + {/* <Text |
| 107 | + accessibilityLabel="Welcome note" |
| 108 | + numberOfLines={1} |
| 109 | + onLongPress={() => alert('Text pressed')} |
| 110 | + > |
| 111 | + Lorem ipsum dolor, sit amet consectetur adipisicing elit. Unde |
| 112 | + perspiciatis nobis non voluptatibus tenetur fuga magni accusamus. |
| 113 | + Atque minima laboriosam |
| 114 | + </Text> |
| 115 | + <TouchableOpacity onPress={() => alert('Image pressed')}> |
| 116 | + <Image |
| 117 | + resizeMode="contain" |
| 118 | + loadingIndicatorSource={require('./assets/adaptive-icon.png')} |
| 119 | + fadeDuration={1299} |
| 120 | + source={{ |
| 121 | + width: 200, |
| 122 | + height: 200, |
| 123 | + uri: 'https://picsum.photos/200', |
| 124 | + }} |
| 125 | + /> |
| 126 | + </TouchableOpacity> |
| 127 | + <TouchableHighlight |
| 128 | + underlayColor="white" |
| 129 | + onPress={() => alert('Image pressed')} |
| 130 | + > |
| 131 | + <Image |
| 132 | + resizeMode="contain" |
| 133 | + loadingIndicatorSource={require('./assets/adaptive-icon.png')} |
| 134 | + fadeDuration={1299} |
| 135 | + source={{ |
| 136 | + width: 200, |
| 137 | + height: 200, |
| 138 | + uri: 'https://picsum.photos/200', |
| 139 | + }} |
| 140 | + /> |
| 141 | + </TouchableHighlight> |
| 142 | + <TextInput style={styles.textInput} placeholder="Type here" /> |
| 143 | + <Button |
| 144 | + onPress={onPressLearnMore} |
| 145 | + title="Learn More" |
| 146 | + color="#841584" |
| 147 | + accessibilityLabel="Learn more about this purple button" |
| 148 | + /> |
| 149 | + <Button |
| 150 | + title="Press me" |
| 151 | + onPress={() => |
| 152 | + Alert.alert('Simple Button pressed', 'Hello', [ |
| 153 | + { |
| 154 | + text: 'Cancel', |
| 155 | + }, |
| 156 | + { |
| 157 | + text: 'OK', |
| 158 | + onPress: () => BackHandler.exitApp(), |
| 159 | + }, |
| 160 | + ]) |
| 161 | + } |
| 162 | + /> |
| 163 | + <Text>ipsum dolor, sit amet consectetu</Text> |
| 164 | + <View |
| 165 | + style={{ backgroundColor: 'dodgerblue', width: '50%', height: 70 }} |
| 166 | + ></View> */} |
| 167 | + <Button |
| 168 | + title="Login with Biometrics" |
| 169 | + color="#000" |
| 170 | + onPress={handleBiometricAuth} |
| 171 | + /> |
| 172 | + <StatusBar style="auto" /> |
| 173 | + </View> |
| 174 | + </ScrollView> |
| 175 | + </SafeAreaView> |
11 | 176 | );
|
12 | 177 | }
|
13 | 178 |
|
14 | 179 | const styles = StyleSheet.create({
|
| 180 | + scrollView: { |
| 181 | + flex: 1, |
| 182 | + alignItems: 'center', |
| 183 | + justifyContent: 'center', |
| 184 | + }, |
15 | 185 | container: {
|
16 | 186 | flex: 1,
|
17 |
| - backgroundColor: '#fff', |
| 187 | + backgroundColor: '#eee', |
18 | 188 | alignItems: 'center',
|
19 | 189 | justifyContent: 'center',
|
20 | 190 | },
|
| 191 | + textInput: {}, |
21 | 192 | });
|
0 commit comments