Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
submit = async () => {
const id = ++this.submitId;
const { password } = this.state;
const { unlock } = this.props;
if (!password) return;
try {
const credentials = await Keychain.getGenericPassword();
if (id !== this.submitId) return;
if (credentials && credentials.password === password) {
unlock();
} else if (credentials) {
Vibration.vibrate(VIBRATION_PATTERN_ERROR);
this.setState({
passwordError: new PasswordIncorrectError(),
password: "",
});
} else {
console.log("no credentials stored"); // eslint-disable-line
}
} catch (err) {
if (id !== this.submitId) return;
console.log("could not load credentials"); // eslint-disable-line
this.setState({ passwordError: err, password: "" });
}
};
disablePassword = (e: SyntheticEvent) => {
if (e) {
e.preventDefault()
}
const { currentPassword } = this.state
const { onChangePassword } = this.props
if (!db.isEncryptionKeyCorrect('app', 'accounts', currentPassword)) {
this.setState({ incorrectPassword: new PasswordIncorrectError() })
return
}
onChangePassword('')
}
const { unlock, fetchAccounts } = this.props
const { inputValue } = this.state
const isAccountsDecrypted = await db.hasBeenDecrypted('app', 'accounts')
try {
if (!isAccountsDecrypted) {
await db.setEncryptionKey('app', 'accounts', inputValue.password)
await fetchAccounts()
} else if (!db.isEncryptionKeyCorrect('app', 'accounts', inputValue.password)) {
throw new PasswordIncorrectError()
}
unlock()
this.setState(defaultState)
} catch (err) {
this.setState({ incorrectPassword: new PasswordIncorrectError() })
}
}
handleSave = (e: SyntheticEvent) => {
const { currentPassword, newPassword } = this.state
if (e) {
e.preventDefault()
}
if (!this.isValid()) {
return
}
const { hasPassword, onChangePassword } = this.props
if (hasPassword) {
if (!db.isEncryptionKeyCorrect('app', 'accounts', currentPassword)) {
this.setState({ incorrectPassword: new PasswordIncorrectError() })
return
}
onChangePassword(newPassword)
} else {
onChangePassword(newPassword)
}
}