You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

97 lines
3.8 KiB

import React, { Component } from 'react';
import { View, Image, TouchableOpacity } from 'react-native';
import { Icon } from 'react-native-elements';
import Biometric from './class/biometrics';
import PropTypes from 'prop-types';
import { SafeAreaView } from 'react-navigation';
/** @type {AppStorage} */
const BlueApp = require('./BlueApp');
export default class UnlockWith extends Component {
state = { biometricType: false, isStorageEncrypted: false, isAuthenticating: false };
async componentDidMount() {
let biometricType = false;
if (await Biometric.isBiometricUseCapableAndEnabled()) {
biometricType = await Biometric.biometricType();
}
const isStorageEncrypted = await BlueApp.storageIsEncrypted();
this.setState({ biometricType, isStorageEncrypted }, async () => {
if (!biometricType || isStorageEncrypted) {
this.handleUnlockWithKey();
} else if (typeof biometricType === 'string') this.handleUnlockWithBiometrics();
});
}
successfullyAuthenticated = () => {
this.props.onSuccessfullyAuthenticated();
};
handleUnlockWithBiometrics = async () => {
if (await BlueApp.storageIsEncrypted()) {
this.handleUnlockWithKey();
}
this.setState({ isAuthenticating: true }, async () => {
if (await Biometric.unlockWithBiometrics()) {
await BlueApp.startAndDecrypt();
return this.props.onSuccessfullyAuthenticated();
}
this.setState({ isAuthenticating: false });
});
};
handleUnlockWithKey = () => {
this.setState({ isAuthenticating: true }, async () => {
await BlueApp.startAndDecrypt();
this.props.onSuccessfullyAuthenticated();
});
};
render() {
if (!this.state.biometricType && !this.state.isStorageEncrypted) {
return <View />;
}
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={{ flex: 2, justifyContent: 'space-between', alignItems: 'center' }}>
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Image source={require('./img/qr-code.png')} style={{ width: 120, height: 120 }} />
</View>
<View style={{ flex: 0.2, justifyContent: 'flex-end', marginBottom: 58 }}>
<View style={{ justifyContent: 'center', flexDirection: 'row' }}>
{this.state.biometricType === Biometric.TouchID && !this.state.isStorageEncrypted && (
<>
<TouchableOpacity disabled={this.state.isAuthenticating} onPress={this.handleUnlockWithBiometrics}>
<Image source={require('./img/fingerprint.png')} style={{ width: 64, height: 64 }} />
</TouchableOpacity>
</>
)}
{this.state.biometricType === Biometric.FaceID && !this.state.isStorageEncrypted && (
<>
<TouchableOpacity disabled={this.state.isAuthenticating} onPress={this.handleUnlockWithBiometrics}>
<Image source={require('./img/faceid.png')} style={{ width: 64, height: 64 }} />
</TouchableOpacity>
</>
)}
{this.state.biometricType !== false && this.state.isStorageEncrypted && (
<View style={{ backgroundColor: 'gray', width: 0.5, height: 20, marginHorizontal: 16 }} />
)}
{this.state.isStorageEncrypted && (
<>
<TouchableOpacity disabled={this.state.isAuthenticating} onPress={this.handleUnlockWithKey}>
<Icon name="key" size={64} type="font-awesome" />
</TouchableOpacity>
</>
)}
</View>
</View>
</View>
</SafeAreaView>
);
}
}
UnlockWith.propTypes = {
onSuccessfullyAuthenticated: PropTypes.func,
};