From 6c4b14ca68924f7afe4715ecabb2e5356b4b129a Mon Sep 17 00:00:00 2001 From: Mustafa Buyukcelebi Date: Tue, 12 Mar 2019 16:18:55 +0300 Subject: [PATCH] Added forgot pin feature --- src/config/locales/en-US.json | 1 + src/realm/realm.js | 18 +++- .../pinCode/container/pinCodeContainer.js | 83 +++++++++++++++---- src/screens/pinCode/screen/pinCodeScreen.js | 4 +- 4 files changed, 84 insertions(+), 22 deletions(-) diff --git a/src/config/locales/en-US.json b/src/config/locales/en-US.json index 6656bf34f..59c133969 100644 --- a/src/config/locales/en-US.json +++ b/src/config/locales/en-US.json @@ -144,6 +144,7 @@ "invalid_pincode": "Invalid PIN code, please check and try again.", "remove_alert": "Are you sure you want to remove?", "clear_alert": "Are you sure you want to clear?", + "clear_user_alert": "Are you sure you want to clear all user data?", "clear": "Clear", "cancel": "Cancel", "delete": "Delete", diff --git a/src/realm/realm.js b/src/realm/realm.js index 93a67d04d..d81108da6 100644 --- a/src/realm/realm.js +++ b/src/realm/realm.js @@ -176,8 +176,7 @@ export const updateUserData = userData => new Promise((resolve, reject) => { export const removeUserData = username => new Promise((resolve, reject) => { try { - const account = realm.objects(USER_SCHEMA).filtered('username = $0', username); - + const account = realm.objects(USER_SCHEMA).filtered('username = $0', username) if (Array.from(account).length > 0) { realm.write(() => { realm.delete(account); @@ -191,6 +190,21 @@ export const removeUserData = username => new Promise((resolve, reject) => { } }); +export const removeAllUserData = () => new Promise((resolve, reject) => { + try { + const accounts = realm.objects(USER_SCHEMA); + const scAccount = realm.objects(SC_ACCOUNTS); + + realm.write(() => { + realm.delete(accounts); + realm.delete(scAccount); + resolve(); + }); + } catch (error) { + reject(error); + } +}); + // // TODO: This method deleting ALL users. This should delete just a user. // export const removeUserData = () => new Promise((resolve, reject) => { // setAuthStatus({ isLoggedIn: false }).then(() => { diff --git a/src/screens/pinCode/container/pinCodeContainer.js b/src/screens/pinCode/container/pinCodeContainer.js index a23912a6b..e8275a3fb 100644 --- a/src/screens/pinCode/container/pinCodeContainer.js +++ b/src/screens/pinCode/container/pinCodeContainer.js @@ -10,11 +10,19 @@ import { verifyPinCode, updatePinCode, } from '../../../providers/steem/auth'; -import { closePinCodeModal } from '../../../redux/actions/applicationActions'; -import { getExistUser, setExistUser, getUserDataWithUsername } from '../../../realm/realm'; +import { closePinCodeModal, login, logoutDone } from '../../../redux/actions/applicationActions'; +import { + getExistUser, + setExistUser, + getUserDataWithUsername, + removeAllUserData, + removePinCode, + setAuthStatus, +} from '../../../realm/realm'; import { updateCurrentAccount, setPinCode as savePinCode, + removeOtherAccount, } from '../../../redux/actions/accountAction'; import { getUser } from '../../../providers/steem/dsteem'; @@ -112,11 +120,14 @@ class PinCodeContainer extends Component { resolve(); }) .catch((err) => { - Alert.alert(intl.formatMessage({ - id: 'alert.warning', - }), intl.formatMessage({ - id: err.message, - })); + Alert.alert( + intl.formatMessage({ + id: 'alert.warning', + }), + intl.formatMessage({ + id: err.message, + }), + ); reject(err); }); } @@ -154,12 +165,7 @@ class PinCodeContainer extends Component { _verifyPinCode = pin => new Promise((resolve, reject) => { const { - currentAccount, - dispatch, - accessToken, - navigateTo, - navigation, - intl, + currentAccount, dispatch, accessToken, navigateTo, navigation, intl, } = this.props; // If the user is exist, we are just checking to pin and navigating to home screen @@ -183,11 +189,14 @@ class PinCodeContainer extends Component { } }) .catch((err) => { - Alert.alert(intl.formatMessage({ - id: 'alert.warning', - }), intl.formatMessage({ - id: err.message, - })); + Alert.alert( + intl.formatMessage({ + id: 'alert.warning', + }), + intl.formatMessage({ + id: err.message, + }), + ); reject(err); }); }); @@ -264,6 +273,42 @@ class PinCodeContainer extends Component { }, 1000); }; + _handleForgotButton = () => { + const { intl } = this.props; + + Alert.alert( + intl.formatMessage({ + id: 'alert.warning', + }), + intl.formatMessage({ + id: 'alert.clear_user_alert', + }), + [ + { text: intl.formatMessage({ id: 'alert.clear' }), onPress: () => this._forgotPinCode() }, + { text: intl.formatMessage({ id: 'alert.cancel' }), style: 'destructive' }, + ] + ); + }; + + _forgotPinCode = async () => { + const { otherAccounts, dispatch } = this.props; + + await removeAllUserData() + .then(async () => { + dispatch(updateCurrentAccount({})); + dispatch(login(false)); + removePinCode(); + setAuthStatus({ isLoggedIn: false }); + setExistUser(false); + if (otherAccounts.length > 0) { + otherAccounts.map(item => dispatch(removeOtherAccount(item.username))); + } + dispatch(logoutDone()); + dispatch(closePinCodeModal()); + }) + .catch(() => {}); + }; + render() { const { currentAccount, intl, isReset } = this.props; const { informationText, isExistUser } = this.state; @@ -274,6 +319,7 @@ class PinCodeContainer extends Component { showForgotButton={isExistUser} username={currentAccount.name} intl={intl} + handleForgotButton={() => this._handleForgotButton()} {...this.props} /> ); @@ -283,6 +329,7 @@ class PinCodeContainer extends Component { const mapStateToProps = state => ({ currentAccount: state.account.currentAccount, applicationPinCode: state.account.pin, + otherAccounts: state.account.otherAccounts, }); export default injectIntl(connect(mapStateToProps)(PinCodeContainer)); diff --git a/src/screens/pinCode/screen/pinCodeScreen.js b/src/screens/pinCode/screen/pinCodeScreen.js index 3abcf19ea..a7e7d6f39 100644 --- a/src/screens/pinCode/screen/pinCodeScreen.js +++ b/src/screens/pinCode/screen/pinCodeScreen.js @@ -51,7 +51,7 @@ class PinCodeScreen extends PureComponent { render() { const { - informationText, showForgotButton, username, intl, + informationText, showForgotButton, username, intl, handleForgotButton, } = this.props; const { pin, loading } = this.state; @@ -73,7 +73,7 @@ class PinCodeScreen extends PureComponent { {showForgotButton ? ( - + handleForgotButton()} style={styles.forgotButtonView}> {intl.formatMessage({ id: 'pincode.forgot_text',