2021-02-15 11:08:05 +05:00
|
|
|
import React, {Component, createRef} from 'react';
|
2021-02-18 11:07:04 +05:00
|
|
|
import {Platform} from 'react-native';
|
|
|
|
|
import {
|
|
|
|
|
InteractionManager,
|
|
|
|
|
TouchableOpacity,
|
|
|
|
|
View,
|
|
|
|
|
Clipboard,
|
|
|
|
|
} from 'react-native';
|
2020-03-02 13:52:09 +05:00
|
|
|
import Share from 'react-native-share';
|
2020-11-17 20:17:50 +05:00
|
|
|
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
|
2021-02-15 11:08:05 +05:00
|
|
|
import {notesnook} from '../../../e2e/test.ids';
|
2021-02-16 13:23:43 +05:00
|
|
|
import BiometricService from '../../services/BiometricService';
|
2021-02-15 11:08:05 +05:00
|
|
|
import {DDS} from '../../services/DeviceDetection';
|
2020-03-10 23:19:16 +05:00
|
|
|
import {
|
2020-11-02 20:45:56 +05:00
|
|
|
eSendEvent,
|
|
|
|
|
eSubscribeEvent,
|
|
|
|
|
eUnSubscribeEvent,
|
2020-12-31 21:11:32 +05:00
|
|
|
sendNoteEditedEvent,
|
2021-02-15 11:08:05 +05:00
|
|
|
ToastEvent,
|
2020-10-13 17:02:14 +05:00
|
|
|
} from '../../services/EventManager';
|
2021-02-15 11:08:05 +05:00
|
|
|
import Navigation from '../../services/Navigation';
|
|
|
|
|
import {getElevation, toTXT} from '../../utils';
|
|
|
|
|
import {db} from '../../utils/DB';
|
2020-03-10 23:19:16 +05:00
|
|
|
import {
|
2021-02-22 10:32:14 +05:00
|
|
|
eClearEditor,
|
2021-02-18 11:07:04 +05:00
|
|
|
eCloseActionSheet,
|
2020-03-21 10:03:11 +05:00
|
|
|
eCloseVaultDialog,
|
2020-03-10 23:19:16 +05:00
|
|
|
eOnLoadNote,
|
|
|
|
|
eOpenVaultDialog,
|
2020-10-13 17:02:14 +05:00
|
|
|
} from '../../utils/Events';
|
2021-02-16 16:11:10 +05:00
|
|
|
import {deleteItems} from '../../utils/functions';
|
2021-02-15 11:08:05 +05:00
|
|
|
import {tabBarRef} from '../../utils/Refs';
|
|
|
|
|
import {ph, SIZE} from '../../utils/SizeUtils';
|
2021-02-18 11:07:04 +05:00
|
|
|
import {sleep} from '../../utils/TimeUtils';
|
2021-02-22 10:48:51 +05:00
|
|
|
import {getNote} from '../../views/Editor/Functions';
|
2021-02-15 11:08:05 +05:00
|
|
|
import {Button} from '../Button';
|
2020-09-27 13:05:26 +05:00
|
|
|
import BaseDialog from '../Dialog/base-dialog';
|
|
|
|
|
import DialogButtons from '../Dialog/dialog-buttons';
|
|
|
|
|
import DialogHeader from '../Dialog/dialog-header';
|
2020-12-20 13:04:05 +05:00
|
|
|
import Input from '../Input';
|
2021-02-15 11:08:05 +05:00
|
|
|
import {Toast} from '../Toast';
|
2020-11-17 20:17:50 +05:00
|
|
|
import Paragraph from '../Typography/Paragraph';
|
|
|
|
|
|
2021-02-11 01:36:12 +05:00
|
|
|
let Keychain;
|
2020-03-03 15:37:48 +05:00
|
|
|
const passInputRef = createRef();
|
2020-03-10 23:19:16 +05:00
|
|
|
const confirmPassRef = createRef();
|
2020-11-25 11:46:44 +05:00
|
|
|
const changePassInputRef = createRef();
|
2020-01-17 21:05:38 +05:00
|
|
|
export class VaultDialog extends Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
visible: false,
|
2020-03-10 23:19:16 +05:00
|
|
|
wrongPassword: false,
|
2020-11-25 11:46:44 +05:00
|
|
|
loading: false,
|
2020-03-10 23:19:16 +05:00
|
|
|
note: {},
|
|
|
|
|
vault: false,
|
|
|
|
|
locked: true,
|
|
|
|
|
permanant: false,
|
|
|
|
|
goToEditor: false,
|
|
|
|
|
share: false,
|
|
|
|
|
passwordsDontMatch: false,
|
2020-03-14 12:13:00 +05:00
|
|
|
deleteNote: false,
|
2020-11-02 20:45:56 +05:00
|
|
|
focusIndex: null,
|
2020-11-17 20:17:50 +05:00
|
|
|
biometricUnlock: false,
|
2020-12-22 00:24:08 +05:00
|
|
|
isBiometryEnrolled: false,
|
2020-11-17 20:17:50 +05:00
|
|
|
isBiometryAvailable: false,
|
|
|
|
|
fingerprintAccess: false,
|
2020-11-25 11:46:44 +05:00
|
|
|
changePassword: false,
|
2020-12-20 13:04:05 +05:00
|
|
|
copyNote: false,
|
2020-12-22 00:24:08 +05:00
|
|
|
revokeFingerprintAccess: false,
|
2021-02-16 16:11:10 +05:00
|
|
|
title: 'Unlock Note',
|
|
|
|
|
description: null,
|
2020-01-17 21:05:38 +05:00
|
|
|
};
|
|
|
|
|
this.password = null;
|
2020-03-17 15:09:15 +05:00
|
|
|
this.confirmPassword = null;
|
2020-11-25 11:46:44 +05:00
|
|
|
this.newPassword = null;
|
2021-02-16 16:11:10 +05:00
|
|
|
(this.title = !this.state.novault
|
|
|
|
|
? 'Create Vault'
|
|
|
|
|
: this.state.fingerprintAccess
|
|
|
|
|
? 'Vault Fingerprint Unlock'
|
|
|
|
|
: this.state.revokeFingerprintAccess
|
|
|
|
|
? 'Revoke Vault Fingerprint Unlock'
|
|
|
|
|
: this.state.changePassword
|
|
|
|
|
? 'Change Vault Password'
|
|
|
|
|
: this.state.note.locked
|
|
|
|
|
? this.state.deleteNote
|
|
|
|
|
? 'Delete note'
|
|
|
|
|
: this.state.share
|
|
|
|
|
? 'Share note'
|
|
|
|
|
: this.state.copyNote
|
|
|
|
|
? 'Copy note'
|
|
|
|
|
: this.state.goToEditor
|
|
|
|
|
? 'Unlock note'
|
|
|
|
|
: 'Unlock note'
|
|
|
|
|
: 'Lock note'),
|
|
|
|
|
(this.description = !this.state.novault
|
|
|
|
|
? 'Set a password to create vault'
|
|
|
|
|
: this.state.fingerprintAccess
|
2021-04-22 10:29:34 +05:00
|
|
|
? 'Enter vault password to enable fingerprint unlocking.'
|
2021-02-16 16:11:10 +05:00
|
|
|
: this.state.revokeFingerprintAccess
|
|
|
|
|
? 'Disable vault fingerprint unlock '
|
|
|
|
|
: this.state.changePassword
|
|
|
|
|
? 'Setup a new password for the vault.'
|
|
|
|
|
: this.state.permanant
|
|
|
|
|
? 'Enter password to remove note from vault.'
|
|
|
|
|
: this.state.note.locked
|
|
|
|
|
? this.state.deleteNote
|
2021-04-22 10:29:34 +05:00
|
|
|
? 'Unlock note to delete it. If biometrics are not working, you can enter device pin to unlock vault.'
|
2021-02-16 16:11:10 +05:00
|
|
|
: this.state.share
|
2021-04-22 10:29:34 +05:00
|
|
|
? 'Unlock note to share it. If biometrics are not working, you can enter device pin to unlock vault.'
|
2021-02-16 16:11:10 +05:00
|
|
|
: this.state.copyNote
|
2021-04-22 10:29:34 +05:00
|
|
|
? 'Unlock note to copy it. If biometrics are not working, you can enter device pin to unlock vault.'
|
2021-02-16 16:11:10 +05:00
|
|
|
: this.state.goToEditor
|
2021-04-22 10:29:34 +05:00
|
|
|
? 'Unlock note to open it in editor. If biometrics are not working, you can enter device pin to unlock vault.'
|
|
|
|
|
: 'Enter vault password to unlock note. If biometrics are not working, you can enter device pin to unlock vault.'
|
|
|
|
|
: 'Enter vault password to lock note. If biometrics are not working, you can enter device pin to lock note.');
|
2020-01-17 21:05:38 +05:00
|
|
|
}
|
2020-01-17 00:23:16 +05:00
|
|
|
|
2020-03-10 23:19:16 +05:00
|
|
|
componentDidMount() {
|
|
|
|
|
eSubscribeEvent(eOpenVaultDialog, this.open);
|
|
|
|
|
eSubscribeEvent(eCloseVaultDialog, this.close);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
eUnSubscribeEvent(eOpenVaultDialog, this.open);
|
|
|
|
|
eUnSubscribeEvent(eCloseVaultDialog, this.close);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 20:17:50 +05:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {import('../../services/EventManager').vaultType} data
|
|
|
|
|
*/
|
2021-04-22 10:29:34 +05:00
|
|
|
open = async data => {
|
2021-02-15 11:08:05 +05:00
|
|
|
if (!Keychain) {
|
|
|
|
|
Keychain = require('react-native-keychain');
|
|
|
|
|
}
|
2021-02-16 13:23:43 +05:00
|
|
|
let biometry = await BiometricService.isBiometryAvailable();
|
2020-11-17 20:17:50 +05:00
|
|
|
let available = false;
|
2021-02-16 13:23:43 +05:00
|
|
|
let fingerprint = await BiometricService.hasInternetCredentials('nn_vault');
|
2020-12-22 00:24:08 +05:00
|
|
|
|
2021-02-16 13:23:43 +05:00
|
|
|
if (biometry) {
|
2020-11-17 20:17:50 +05:00
|
|
|
available = true;
|
|
|
|
|
}
|
2021-02-22 10:48:51 +05:00
|
|
|
|
2020-01-17 21:05:38 +05:00
|
|
|
this.setState({
|
2020-11-17 20:17:50 +05:00
|
|
|
note: data.item,
|
|
|
|
|
novault: data.novault,
|
|
|
|
|
locked: data.locked,
|
|
|
|
|
permanant: data.permanant,
|
|
|
|
|
goToEditor: data.goToEditor,
|
|
|
|
|
share: data.share,
|
|
|
|
|
deleteNote: data.deleteNote,
|
|
|
|
|
copyNote: data.copyNote,
|
|
|
|
|
isBiometryAvailable: available,
|
|
|
|
|
biometricUnlock: fingerprint,
|
2020-12-22 00:24:08 +05:00
|
|
|
isBiometryEnrolled: fingerprint,
|
2020-11-17 20:17:50 +05:00
|
|
|
fingerprintAccess: data.fingerprintAccess,
|
2020-11-25 11:46:44 +05:00
|
|
|
changePassword: data.changePassword,
|
2020-12-22 00:24:08 +05:00
|
|
|
revokeFingerprintAccess: data.revokeFingerprintAccess,
|
2021-02-16 16:11:10 +05:00
|
|
|
title: data.title,
|
|
|
|
|
description: data.description,
|
2020-01-17 21:05:38 +05:00
|
|
|
});
|
2020-12-22 00:24:08 +05:00
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
fingerprint &&
|
|
|
|
|
data.novault &&
|
|
|
|
|
!data.fingerprintAccess &&
|
2021-02-16 17:21:44 +05:00
|
|
|
!data.revokeFingerprintAccess &&
|
|
|
|
|
!data.changePassword
|
2020-12-22 00:24:08 +05:00
|
|
|
) {
|
2021-02-16 16:11:10 +05:00
|
|
|
await this._onPressFingerprintAuth(data.title, data.description);
|
2020-12-22 00:24:08 +05:00
|
|
|
} else {
|
|
|
|
|
this.setState({
|
|
|
|
|
visible: true,
|
|
|
|
|
});
|
2020-11-17 20:17:50 +05:00
|
|
|
}
|
2020-01-17 21:05:38 +05:00
|
|
|
};
|
2020-03-03 15:33:33 +05:00
|
|
|
|
2020-03-10 23:19:16 +05:00
|
|
|
close = () => {
|
2020-11-25 11:46:44 +05:00
|
|
|
if (this.state.loading) {
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
title: this.state.title,
|
|
|
|
|
message: 'Please wait and do not close the app.',
|
|
|
|
|
type: 'success',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2020-11-25 11:46:44 +05:00
|
|
|
return;
|
|
|
|
|
}
|
2021-04-07 11:18:11 +05:00
|
|
|
sendNoteEditedEvent({
|
|
|
|
|
id: this.state.note.id,
|
|
|
|
|
forced: true,
|
|
|
|
|
});
|
2021-02-16 13:23:43 +05:00
|
|
|
Navigation.setRoutesToUpdate([
|
|
|
|
|
Navigation.routeNames.Notes,
|
|
|
|
|
Navigation.routeNames.Favorites,
|
|
|
|
|
Navigation.routeNames.NotesPage,
|
|
|
|
|
Navigation.routeNames.Notebook,
|
|
|
|
|
]);
|
2020-03-19 11:05:52 +05:00
|
|
|
this.password = null;
|
|
|
|
|
this.confirmPassword = null;
|
2020-03-02 13:52:09 +05:00
|
|
|
this.setState({
|
|
|
|
|
visible: false,
|
2020-03-10 23:19:16 +05:00
|
|
|
note: {},
|
|
|
|
|
locked: false,
|
|
|
|
|
permanant: false,
|
|
|
|
|
goToEditor: false,
|
|
|
|
|
share: false,
|
|
|
|
|
novault: false,
|
2020-03-14 12:13:00 +05:00
|
|
|
deleteNote: false,
|
2020-03-19 11:05:52 +05:00
|
|
|
passwordsDontMatch: false,
|
2020-03-02 13:52:09 +05:00
|
|
|
});
|
2020-01-17 21:05:38 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onPress = async () => {
|
2020-12-22 00:24:08 +05:00
|
|
|
if (this.state.revokeFingerprintAccess) {
|
|
|
|
|
await this._revokeFingerprintAccess();
|
2020-12-31 21:40:19 +05:00
|
|
|
this.close();
|
2020-12-22 00:24:08 +05:00
|
|
|
return;
|
|
|
|
|
}
|
2020-11-25 11:46:44 +05:00
|
|
|
if (this.state.loading) return;
|
2020-12-20 13:04:05 +05:00
|
|
|
|
2020-11-25 12:09:07 +05:00
|
|
|
if (!this.password) {
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
title: 'Password not entered',
|
|
|
|
|
message: 'Enter a password for the vault and try again.',
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2020-12-20 13:04:05 +05:00
|
|
|
return;
|
2020-11-25 12:09:07 +05:00
|
|
|
}
|
2020-12-20 13:04:05 +05:00
|
|
|
if (this.password && this.password.length < 3) {
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
title: 'Password too short',
|
|
|
|
|
message: 'Password must be longer than 3 characters.',
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2020-03-14 12:59:52 +05:00
|
|
|
|
2020-12-20 13:04:05 +05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.state.novault) {
|
2020-03-17 15:09:15 +05:00
|
|
|
if (this.password !== this.confirmPassword) {
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
title: 'Passwords do not match',
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2020-03-17 15:09:15 +05:00
|
|
|
this.setState({
|
|
|
|
|
passwordsDontMatch: true,
|
|
|
|
|
});
|
2020-03-10 23:19:16 +05:00
|
|
|
return;
|
|
|
|
|
}
|
2020-03-17 15:09:15 +05:00
|
|
|
|
|
|
|
|
this._createVault();
|
2020-11-25 11:46:44 +05:00
|
|
|
} else if (this.state.changePassword) {
|
|
|
|
|
this.setState({
|
|
|
|
|
loading: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
db.vault
|
|
|
|
|
.changePassword(this.password, this.newPassword)
|
2021-04-22 10:29:34 +05:00
|
|
|
.then(result => {
|
2020-11-25 11:46:44 +05:00
|
|
|
this.setState({
|
|
|
|
|
loading: false,
|
|
|
|
|
});
|
|
|
|
|
if (this.state.biometricUnlock) {
|
|
|
|
|
this._enrollFingerprint(this.newPassword);
|
|
|
|
|
}
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
title: 'Vault password updated',
|
|
|
|
|
type: 'success',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2020-11-25 11:46:44 +05:00
|
|
|
this.close();
|
|
|
|
|
})
|
2021-04-22 10:29:34 +05:00
|
|
|
.catch(e => {
|
2020-11-25 11:46:44 +05:00
|
|
|
this.setState({
|
|
|
|
|
loading: false,
|
|
|
|
|
});
|
2020-11-25 12:10:25 +05:00
|
|
|
if (e.message === db.vault.ERRORS.wrongPassword) {
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
title: 'Incorrect password',
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2020-11-25 11:46:44 +05:00
|
|
|
}
|
|
|
|
|
});
|
2020-03-10 23:19:16 +05:00
|
|
|
} else if (this.state.locked) {
|
|
|
|
|
if (!this.password || this.password.trim() === 0) {
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
title: 'Incorrect password',
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2020-03-14 12:13:00 +05:00
|
|
|
this.setState({
|
|
|
|
|
wrongPassword: true,
|
|
|
|
|
});
|
2020-03-03 15:44:31 +05:00
|
|
|
return;
|
2020-01-17 21:05:38 +05:00
|
|
|
}
|
2020-12-20 13:04:05 +05:00
|
|
|
db.vault
|
|
|
|
|
.unlock(this.password)
|
|
|
|
|
.then(async () => {
|
|
|
|
|
this.setState({
|
|
|
|
|
wrongPassword: false,
|
|
|
|
|
});
|
|
|
|
|
if (this.state.note.locked) {
|
|
|
|
|
await this._unlockNote();
|
|
|
|
|
} else {
|
|
|
|
|
await this._lockNote();
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-04-22 10:29:34 +05:00
|
|
|
.catch(e => {
|
2020-12-20 13:04:05 +05:00
|
|
|
this._takeErrorAction(e);
|
|
|
|
|
});
|
2020-12-31 21:40:19 +05:00
|
|
|
} else if (this.state.fingerprintAccess) {
|
2020-11-25 11:46:44 +05:00
|
|
|
this._enrollFingerprint(this.password);
|
2020-03-10 23:19:16 +05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async _lockNote() {
|
|
|
|
|
if (!this.password || this.password.trim() === 0) {
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
title: 'Incorrect password',
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2021-02-22 10:48:51 +05:00
|
|
|
console.log('returning from here');
|
2020-03-10 23:19:16 +05:00
|
|
|
return;
|
|
|
|
|
} else {
|
2021-02-22 10:48:51 +05:00
|
|
|
await db.vault.add(this.state.note.id);
|
2021-04-07 11:20:47 +05:00
|
|
|
if (this.state.note.id === getNote()?.id) {
|
2021-02-22 10:48:51 +05:00
|
|
|
eSendEvent(eClearEditor);
|
|
|
|
|
}
|
|
|
|
|
this.close();
|
|
|
|
|
this.setState({
|
|
|
|
|
loading: false,
|
2020-03-10 23:19:16 +05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-03 15:33:33 +05:00
|
|
|
|
2020-03-10 23:19:16 +05:00
|
|
|
async _unlockNote() {
|
|
|
|
|
if (!this.password || this.password.trim() === 0) {
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
title: 'Incorrect password',
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2020-03-10 23:19:16 +05:00
|
|
|
return;
|
2020-12-22 00:24:08 +05:00
|
|
|
}
|
|
|
|
|
if (this.state.permanant) {
|
|
|
|
|
this._permanantUnlock();
|
2020-03-10 23:19:16 +05:00
|
|
|
} else {
|
2020-12-22 00:24:08 +05:00
|
|
|
await this._openNote();
|
2020-03-10 23:19:16 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 21:29:30 +05:00
|
|
|
_openNote = async () => {
|
|
|
|
|
try {
|
|
|
|
|
let note = await db.vault.open(this.state.note.id, this.password);
|
|
|
|
|
if (this.state.biometricUnlock && !this.state.isBiometryEnrolled) {
|
|
|
|
|
await this._enrollFingerprint(this.password);
|
|
|
|
|
}
|
|
|
|
|
if (this.state.goToEditor) {
|
|
|
|
|
this._openInEditor(note);
|
|
|
|
|
} else if (this.state.share) {
|
2020-12-31 21:33:14 +05:00
|
|
|
await this._shareNote(note);
|
2020-12-31 21:29:30 +05:00
|
|
|
} else if (this.state.deleteNote) {
|
|
|
|
|
await this._deleteNote();
|
|
|
|
|
} else if (this.state.copyNote) {
|
|
|
|
|
this._copyNote(note);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this._takeErrorAction(e);
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-03-14 12:13:00 +05:00
|
|
|
async _deleteNote() {
|
2021-02-16 16:11:10 +05:00
|
|
|
try {
|
|
|
|
|
await db.vault.remove(this.state.note.id, this.password);
|
|
|
|
|
await deleteItems(this.state.note);
|
|
|
|
|
this.close();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this._takeErrorAction(e);
|
|
|
|
|
}
|
2020-03-10 23:19:16 +05:00
|
|
|
}
|
|
|
|
|
|
2020-11-25 11:46:44 +05:00
|
|
|
async _enrollFingerprint(password) {
|
2020-11-17 20:17:50 +05:00
|
|
|
try {
|
2021-01-01 15:37:47 +05:00
|
|
|
this.setState(
|
|
|
|
|
{
|
|
|
|
|
loading: true,
|
|
|
|
|
},
|
|
|
|
|
async () => {
|
2021-04-22 10:29:34 +05:00
|
|
|
try {
|
|
|
|
|
await db.vault.unlock(password);
|
|
|
|
|
await BiometricService.storeCredentials(password);
|
|
|
|
|
this.setState({
|
|
|
|
|
loading: false,
|
|
|
|
|
});
|
|
|
|
|
eSendEvent('vaultUpdated');
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Biometric unlocking enabled!',
|
|
|
|
|
message: 'Now you can unlock your notes with biometrics.',
|
|
|
|
|
type: 'success',
|
|
|
|
|
context: 'global',
|
|
|
|
|
});
|
|
|
|
|
this.close();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Password is incorrect!',
|
|
|
|
|
message:
|
|
|
|
|
'Please enter the correct vault password to enable biometrics.',
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
|
|
|
|
this.setState({
|
|
|
|
|
loading: false,
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-01-01 15:37:47 +05:00
|
|
|
},
|
|
|
|
|
);
|
2020-11-17 20:17:50 +05:00
|
|
|
} catch (e) {
|
|
|
|
|
this._takeErrorAction(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 23:19:16 +05:00
|
|
|
async _createVault() {
|
|
|
|
|
await db.vault.create(this.password);
|
2021-02-16 13:23:43 +05:00
|
|
|
|
2020-11-17 20:17:50 +05:00
|
|
|
if (this.state.biometricUnlock) {
|
2021-02-16 13:23:43 +05:00
|
|
|
await this._enrollFingerprint(this.password);
|
2020-11-17 20:17:50 +05:00
|
|
|
}
|
2021-04-07 11:18:11 +05:00
|
|
|
if (this.state.note?.id) {
|
2020-03-10 23:19:16 +05:00
|
|
|
await db.vault.add(this.state.note.id);
|
2021-04-07 11:18:11 +05:00
|
|
|
if (this.state.note.id === getNote()?.id) {
|
2021-02-22 10:32:14 +05:00
|
|
|
eSendEvent(eClearEditor);
|
|
|
|
|
}
|
2021-02-16 13:23:43 +05:00
|
|
|
this.setState({
|
|
|
|
|
loading: false,
|
|
|
|
|
});
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
2021-04-22 10:29:34 +05:00
|
|
|
heading: 'Note added to vault',
|
2021-02-20 15:03:02 +05:00
|
|
|
type: 'success',
|
2021-04-07 11:18:11 +05:00
|
|
|
context: 'global',
|
2021-02-20 15:03:02 +05:00
|
|
|
});
|
2021-02-16 13:23:43 +05:00
|
|
|
this.close();
|
2020-11-17 20:17:50 +05:00
|
|
|
} else {
|
|
|
|
|
eSendEvent('vaultUpdated');
|
|
|
|
|
this.close();
|
2020-03-10 23:19:16 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_permanantUnlock() {
|
|
|
|
|
db.vault
|
|
|
|
|
.remove(this.state.note.id, this.password)
|
2021-04-22 10:29:34 +05:00
|
|
|
.then(r => {
|
2020-03-10 23:19:16 +05:00
|
|
|
this.close();
|
|
|
|
|
})
|
2021-04-22 10:29:34 +05:00
|
|
|
.catch(e => {
|
2020-03-10 23:19:16 +05:00
|
|
|
this._takeErrorAction(e);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_openInEditor(note) {
|
|
|
|
|
this.close();
|
2020-12-31 21:33:14 +05:00
|
|
|
InteractionManager.runAfterInteractions(() => {
|
|
|
|
|
eSendEvent(eOnLoadNote, note);
|
|
|
|
|
if (!DDS.isTab) {
|
|
|
|
|
tabBarRef.current?.goToPage(1);
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-03-10 23:19:16 +05:00
|
|
|
}
|
|
|
|
|
|
2020-12-07 11:26:30 +05:00
|
|
|
_copyNote(note) {
|
|
|
|
|
let text = toTXT(note.content.data);
|
2021-02-16 16:11:10 +05:00
|
|
|
text = `${note.title}\n \n ${text}`;
|
2021-02-18 11:07:04 +05:00
|
|
|
console.log(text, 'TEXT');
|
2020-12-07 11:26:30 +05:00
|
|
|
Clipboard.setString(text);
|
2021-02-20 15:03:02 +05:00
|
|
|
Toast.show({
|
|
|
|
|
heading: 'Note copied',
|
|
|
|
|
type: 'success',
|
|
|
|
|
message: 'Note has been copied to clipboard!',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2020-12-07 11:26:30 +05:00
|
|
|
this.close();
|
2020-12-01 16:36:19 +05:00
|
|
|
}
|
|
|
|
|
|
2020-12-31 21:33:14 +05:00
|
|
|
async _shareNote(note) {
|
|
|
|
|
this.close();
|
2020-12-07 11:26:30 +05:00
|
|
|
let text = toTXT(note.content.data);
|
2021-02-16 16:11:10 +05:00
|
|
|
text = `${note.title}\n \n ${text}`;
|
2020-12-31 21:33:14 +05:00
|
|
|
try {
|
|
|
|
|
await Share.open({
|
2021-04-22 10:29:34 +05:00
|
|
|
heading: 'Share note to',
|
2020-12-31 21:33:14 +05:00
|
|
|
failOnCancel: false,
|
2021-02-16 16:11:10 +05:00
|
|
|
message: text,
|
2020-12-31 21:33:14 +05:00
|
|
|
});
|
2021-02-15 11:08:05 +05:00
|
|
|
} catch (e) {}
|
2020-03-10 23:19:16 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_takeErrorAction(e) {
|
|
|
|
|
if (e.message === db.vault.ERRORS.wrongPassword) {
|
|
|
|
|
this.setState({
|
|
|
|
|
wrongPassword: true,
|
2021-04-22 10:29:34 +05:00
|
|
|
visible: true,
|
2020-03-10 23:19:16 +05:00
|
|
|
});
|
2021-04-22 10:29:34 +05:00
|
|
|
setTimeout(() => {
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Incorrect password',
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
|
|
|
|
}, 500);
|
|
|
|
|
|
2020-03-10 23:19:16 +05:00
|
|
|
return;
|
2021-02-22 10:48:51 +05:00
|
|
|
}
|
2020-03-10 23:19:16 +05:00
|
|
|
}
|
2020-01-17 21:05:38 +05:00
|
|
|
|
2020-12-22 00:24:08 +05:00
|
|
|
_revokeFingerprintAccess = async () => {
|
|
|
|
|
try {
|
2021-02-16 13:23:43 +05:00
|
|
|
await BiometricService.resetCredentials();
|
2020-12-22 00:24:08 +05:00
|
|
|
eSendEvent('vaultUpdated');
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
2021-04-22 10:29:34 +05:00
|
|
|
heading: 'Biometric unlocking disabled!',
|
2021-02-20 15:03:02 +05:00
|
|
|
type: 'success',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2020-12-22 00:24:08 +05:00
|
|
|
} catch (e) {
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
2021-04-22 10:29:34 +05:00
|
|
|
heading: 'Failed to disable Biometric unlocking.',
|
|
|
|
|
description: e.message,
|
2021-02-20 15:03:02 +05:00
|
|
|
type: 'success',
|
|
|
|
|
context: 'local',
|
|
|
|
|
});
|
2020-12-22 00:24:08 +05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-16 16:11:10 +05:00
|
|
|
_onPressFingerprintAuth = async (title, description) => {
|
2020-11-17 20:17:50 +05:00
|
|
|
try {
|
2021-02-16 16:11:10 +05:00
|
|
|
let credentials = await BiometricService.getCredentials(
|
|
|
|
|
title || this.state.title,
|
|
|
|
|
description || this.state.description,
|
|
|
|
|
);
|
2021-02-16 13:23:43 +05:00
|
|
|
|
2020-11-17 20:17:50 +05:00
|
|
|
if (credentials?.password) {
|
|
|
|
|
this.password = credentials.password;
|
|
|
|
|
this.onPress();
|
2021-02-16 13:23:43 +05:00
|
|
|
} else {
|
2021-02-18 11:07:04 +05:00
|
|
|
eSendEvent(eCloseActionSheet);
|
|
|
|
|
await sleep(300);
|
2021-02-16 13:23:43 +05:00
|
|
|
this.setState({
|
|
|
|
|
visible: true,
|
|
|
|
|
});
|
2020-11-17 20:17:50 +05:00
|
|
|
}
|
2021-02-16 13:23:43 +05:00
|
|
|
} catch (e) {}
|
2020-11-17 20:17:50 +05:00
|
|
|
};
|
|
|
|
|
|
2020-01-17 21:05:38 +05:00
|
|
|
render() {
|
2020-09-09 11:10:35 +05:00
|
|
|
const {colors} = this.props;
|
2020-03-10 23:19:16 +05:00
|
|
|
const {
|
|
|
|
|
note,
|
|
|
|
|
visible,
|
|
|
|
|
wrongPassword,
|
|
|
|
|
passwordsDontMatch,
|
|
|
|
|
novault,
|
|
|
|
|
locked,
|
|
|
|
|
permanant,
|
2020-11-17 20:17:50 +05:00
|
|
|
biometricUnlock,
|
2020-11-25 11:46:44 +05:00
|
|
|
deleteNote,
|
|
|
|
|
share,
|
|
|
|
|
goToEditor,
|
|
|
|
|
fingerprintAccess,
|
|
|
|
|
changePassword,
|
|
|
|
|
copyNote,
|
|
|
|
|
loading,
|
2020-03-10 23:19:16 +05:00
|
|
|
} = this.state;
|
2020-01-17 21:05:38 +05:00
|
|
|
|
2020-11-23 12:32:33 +05:00
|
|
|
if (!visible) return null;
|
2020-01-17 21:05:38 +05:00
|
|
|
return (
|
2020-09-27 13:05:26 +05:00
|
|
|
<BaseDialog
|
2021-02-22 10:48:51 +05:00
|
|
|
onShow={async () => {
|
2021-04-07 11:18:11 +05:00
|
|
|
await sleep(100);
|
2020-03-03 15:37:48 +05:00
|
|
|
passInputRef.current?.focus();
|
|
|
|
|
}}
|
2020-11-25 11:46:44 +05:00
|
|
|
statusBarTranslucent={false}
|
2020-09-27 13:05:26 +05:00
|
|
|
onRequestClose={this.close}
|
2020-11-23 12:32:33 +05:00
|
|
|
visible={true}>
|
2019-12-07 08:41:48 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
2020-09-27 13:05:26 +05:00
|
|
|
...getElevation(5),
|
2020-12-22 10:27:51 +05:00
|
|
|
width: DDS.isTab ? 350 : '85%',
|
2020-09-27 13:05:26 +05:00
|
|
|
borderRadius: 5,
|
|
|
|
|
backgroundColor: colors.bg,
|
|
|
|
|
paddingHorizontal: ph,
|
2020-12-22 00:24:08 +05:00
|
|
|
paddingVertical: 15,
|
2019-12-07 08:41:48 +05:00
|
|
|
}}>
|
2020-09-27 13:05:26 +05:00
|
|
|
<DialogHeader
|
2021-02-16 16:11:10 +05:00
|
|
|
title={this.state.title}
|
|
|
|
|
paragraph={this.state.description}
|
2020-09-27 13:05:26 +05:00
|
|
|
icon="shield"
|
|
|
|
|
/>
|
|
|
|
|
|
2020-12-22 00:24:08 +05:00
|
|
|
{(novault || changePassword) &&
|
|
|
|
|
!this.state.revokeFingerprintAccess ? (
|
2020-11-17 20:17:50 +05:00
|
|
|
<>
|
2020-12-20 13:04:05 +05:00
|
|
|
<Input
|
|
|
|
|
fwdRef={passInputRef}
|
2020-11-25 11:46:44 +05:00
|
|
|
editable={!loading}
|
|
|
|
|
autoCapitalize="none"
|
2020-12-01 22:52:01 +05:00
|
|
|
testID={notesnook.ids.dialogs.vault.pwd}
|
2021-04-22 10:29:34 +05:00
|
|
|
onChangeText={value => {
|
2020-11-17 20:17:50 +05:00
|
|
|
this.password = value;
|
|
|
|
|
}}
|
2020-12-22 10:27:51 +05:00
|
|
|
marginBottom={
|
|
|
|
|
!this.state.biometricUnlock ||
|
|
|
|
|
!this.state.isBiometryEnrolled ||
|
|
|
|
|
!novault ||
|
|
|
|
|
changePassword
|
|
|
|
|
? 0
|
|
|
|
|
: 10
|
|
|
|
|
}
|
2021-04-05 10:25:05 +05:00
|
|
|
onSubmit={() => {
|
|
|
|
|
changePassword
|
|
|
|
|
? changePassInputRef.current?.focus()
|
|
|
|
|
: this.onPress;
|
|
|
|
|
}}
|
|
|
|
|
returnKeyLabel={changePassword ? 'Next' : this.state.title}
|
|
|
|
|
returnKeyType={changePassword ? 'next' : 'done'}
|
2020-11-17 20:17:50 +05:00
|
|
|
secureTextEntry
|
2021-02-16 16:11:10 +05:00
|
|
|
placeholder={changePassword ? 'Current password' : 'Password'}
|
2020-11-17 20:17:50 +05:00
|
|
|
/>
|
2020-11-25 11:46:44 +05:00
|
|
|
|
|
|
|
|
{!this.state.biometricUnlock ||
|
2020-12-22 00:24:08 +05:00
|
|
|
!this.state.isBiometryEnrolled ||
|
2020-11-25 11:46:44 +05:00
|
|
|
!novault ||
|
|
|
|
|
changePassword ? null : (
|
2020-11-17 20:17:50 +05:00
|
|
|
<Button
|
2021-04-22 10:29:34 +05:00
|
|
|
onPress={() =>
|
|
|
|
|
this._onPressFingerprintAuth('Unlock note', '')
|
|
|
|
|
}
|
2021-04-22 11:49:32 +05:00
|
|
|
icon="fingerprint"
|
2020-11-17 20:17:50 +05:00
|
|
|
width="100%"
|
2021-02-16 16:11:10 +05:00
|
|
|
title={'Biometric unlock'}
|
2020-11-17 20:17:50 +05:00
|
|
|
type="shade"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2020-09-27 13:05:26 +05:00
|
|
|
) : null}
|
2020-01-05 18:03:40 +05:00
|
|
|
|
2020-11-25 11:46:44 +05:00
|
|
|
{changePassword ? (
|
|
|
|
|
<>
|
2020-12-20 13:04:05 +05:00
|
|
|
<Input
|
2020-11-25 11:46:44 +05:00
|
|
|
ref={changePassInputRef}
|
|
|
|
|
editable={!loading}
|
2020-12-01 22:52:01 +05:00
|
|
|
testID={notesnook.ids.dialogs.vault.changePwd}
|
2020-11-25 11:46:44 +05:00
|
|
|
autoCapitalize="none"
|
2021-04-22 10:29:34 +05:00
|
|
|
onChangeText={value => {
|
2020-11-25 11:46:44 +05:00
|
|
|
this.newPassword = value;
|
|
|
|
|
}}
|
2021-04-05 10:25:05 +05:00
|
|
|
onSubmit={this.onPress}
|
|
|
|
|
returnKeyLabel="Change"
|
|
|
|
|
returnKeyType="done"
|
2020-11-25 11:46:44 +05:00
|
|
|
secureTextEntry
|
2021-02-16 16:11:10 +05:00
|
|
|
placeholder={'New password'}
|
2020-11-25 11:46:44 +05:00
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
) : null}
|
|
|
|
|
|
2020-09-27 13:05:26 +05:00
|
|
|
{!novault ? (
|
|
|
|
|
<View>
|
2020-12-20 13:04:05 +05:00
|
|
|
<Input
|
|
|
|
|
fwdRef={passInputRef}
|
2020-11-25 11:46:44 +05:00
|
|
|
autoCapitalize="none"
|
2020-12-01 22:52:01 +05:00
|
|
|
testID={notesnook.ids.dialogs.vault.pwd}
|
2021-04-22 10:29:34 +05:00
|
|
|
onChangeText={value => {
|
2020-03-03 15:33:33 +05:00
|
|
|
this.password = value;
|
2020-01-17 21:05:38 +05:00
|
|
|
}}
|
2021-04-05 10:25:05 +05:00
|
|
|
returnKeyLabel="Next"
|
|
|
|
|
returnKeyType="next"
|
2020-01-17 21:05:38 +05:00
|
|
|
secureTextEntry
|
2021-04-05 10:25:05 +05:00
|
|
|
onSubmit={() => {
|
|
|
|
|
confirmPassRef.current?.focus();
|
|
|
|
|
}}
|
2019-12-07 08:41:48 +05:00
|
|
|
placeholder="Password"
|
|
|
|
|
/>
|
2020-09-09 11:10:35 +05:00
|
|
|
|
2020-12-20 13:04:05 +05:00
|
|
|
<Input
|
|
|
|
|
fwdRef={confirmPassRef}
|
2020-11-25 11:46:44 +05:00
|
|
|
autoCapitalize="none"
|
2020-12-01 22:52:01 +05:00
|
|
|
testID={notesnook.ids.dialogs.vault.pwdAlt}
|
2020-09-27 13:05:26 +05:00
|
|
|
secureTextEntry
|
2020-12-21 23:35:33 +05:00
|
|
|
validationType="confirmPassword"
|
|
|
|
|
customValidator={() => this.password}
|
|
|
|
|
errorMessage="Passwords do not match."
|
2021-04-22 10:29:34 +05:00
|
|
|
onErrorCheck={e => null}
|
2020-12-21 23:35:33 +05:00
|
|
|
marginBottom={0}
|
2021-04-05 10:25:05 +05:00
|
|
|
returnKeyLabel="Create"
|
|
|
|
|
returnKeyType="done"
|
2021-04-22 10:29:34 +05:00
|
|
|
onChangeText={value => {
|
2020-09-27 13:05:26 +05:00
|
|
|
this.confirmPassword = value;
|
|
|
|
|
if (value !== this.password) {
|
|
|
|
|
this.setState({
|
|
|
|
|
passwordsDontMatch: true,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.setState({
|
|
|
|
|
passwordsDontMatch: false,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}}
|
2021-04-05 10:25:05 +05:00
|
|
|
onSubmit={this.onPress}
|
2020-09-27 13:05:26 +05:00
|
|
|
placeholder="Confirm password"
|
2020-09-09 11:10:35 +05:00
|
|
|
/>
|
2020-01-17 21:05:38 +05:00
|
|
|
</View>
|
2020-09-27 13:05:26 +05:00
|
|
|
) : null}
|
|
|
|
|
|
2021-02-16 13:23:43 +05:00
|
|
|
{this.state.biometricUnlock &&
|
|
|
|
|
!this.state.isBiometryEnrolled &&
|
|
|
|
|
novault && (
|
|
|
|
|
<Paragraph>
|
2021-04-22 11:49:32 +05:00
|
|
|
Unlock with password once to enable biometric access.
|
2021-02-16 13:23:43 +05:00
|
|
|
</Paragraph>
|
|
|
|
|
)}
|
2020-12-22 00:24:08 +05:00
|
|
|
|
2020-11-23 12:32:33 +05:00
|
|
|
{this.state.isBiometryAvailable &&
|
|
|
|
|
!this.state.fingerprintAccess &&
|
2020-11-25 11:46:44 +05:00
|
|
|
((!this.state.biometricUnlock && !changePassword) || !novault) ? (
|
2021-04-22 11:49:32 +05:00
|
|
|
<Button
|
2020-11-17 20:17:50 +05:00
|
|
|
onPress={() => {
|
|
|
|
|
this.setState({
|
|
|
|
|
biometricUnlock: !biometricUnlock,
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
style={{
|
2021-04-22 11:49:32 +05:00
|
|
|
marginTop: 10,
|
|
|
|
|
}}
|
|
|
|
|
icon="fingerprint"
|
|
|
|
|
width="100%"
|
|
|
|
|
title="Enable biometric unlocking"
|
|
|
|
|
type="shade"
|
|
|
|
|
/>
|
2020-11-17 20:17:50 +05:00
|
|
|
) : null}
|
|
|
|
|
|
2020-09-27 13:05:26 +05:00
|
|
|
<DialogButtons
|
|
|
|
|
onPressNegative={this.close}
|
|
|
|
|
onPressPositive={this.onPress}
|
2020-11-25 11:46:44 +05:00
|
|
|
loading={loading}
|
2020-09-27 13:05:26 +05:00
|
|
|
positiveTitle={
|
2020-11-25 11:46:44 +05:00
|
|
|
fingerprintAccess
|
2020-11-17 20:17:50 +05:00
|
|
|
? 'Enable'
|
2020-12-22 00:24:08 +05:00
|
|
|
: this.state.revokeFingerprintAccess
|
|
|
|
|
? 'Revoke'
|
2020-11-25 11:46:44 +05:00
|
|
|
: changePassword
|
|
|
|
|
? 'Change'
|
2020-11-17 20:17:50 +05:00
|
|
|
: note.locked
|
2020-11-25 11:46:44 +05:00
|
|
|
? deleteNote
|
2020-09-27 13:05:26 +05:00
|
|
|
? 'Delete'
|
2020-11-25 11:46:44 +05:00
|
|
|
: share
|
2020-09-27 13:05:26 +05:00
|
|
|
? 'Share '
|
2020-11-25 11:46:44 +05:00
|
|
|
: goToEditor
|
2020-09-27 13:05:26 +05:00
|
|
|
? 'Open'
|
|
|
|
|
: 'Unlock'
|
2020-11-17 20:17:50 +05:00
|
|
|
: !note.id
|
|
|
|
|
? 'Create'
|
2020-09-27 13:05:26 +05:00
|
|
|
: 'Lock'
|
|
|
|
|
}
|
|
|
|
|
/>
|
2019-12-07 08:41:48 +05:00
|
|
|
</View>
|
2021-01-01 15:43:41 +05:00
|
|
|
<Toast context="local" />
|
2020-09-27 13:05:26 +05:00
|
|
|
</BaseDialog>
|
2020-01-17 21:05:38 +05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|