Added forgot pin feature

This commit is contained in:
Mustafa Buyukcelebi 2019-03-12 16:18:55 +03:00
parent b0377bb332
commit 6c4b14ca68
4 changed files with 84 additions and 22 deletions

View File

@ -144,6 +144,7 @@
"invalid_pincode": "Invalid PIN code, please check and try again.", "invalid_pincode": "Invalid PIN code, please check and try again.",
"remove_alert": "Are you sure you want to remove?", "remove_alert": "Are you sure you want to remove?",
"clear_alert": "Are you sure you want to clear?", "clear_alert": "Are you sure you want to clear?",
"clear_user_alert": "Are you sure you want to clear all user data?",
"clear": "Clear", "clear": "Clear",
"cancel": "Cancel", "cancel": "Cancel",
"delete": "Delete", "delete": "Delete",

View File

@ -176,8 +176,7 @@ export const updateUserData = userData => new Promise((resolve, reject) => {
export const removeUserData = username => new Promise((resolve, reject) => { export const removeUserData = username => new Promise((resolve, reject) => {
try { 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) { if (Array.from(account).length > 0) {
realm.write(() => { realm.write(() => {
realm.delete(account); 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. // // TODO: This method deleting ALL users. This should delete just a user.
// export const removeUserData = () => new Promise((resolve, reject) => { // export const removeUserData = () => new Promise((resolve, reject) => {
// setAuthStatus({ isLoggedIn: false }).then(() => { // setAuthStatus({ isLoggedIn: false }).then(() => {

View File

@ -10,11 +10,19 @@ import {
verifyPinCode, verifyPinCode,
updatePinCode, updatePinCode,
} from '../../../providers/steem/auth'; } from '../../../providers/steem/auth';
import { closePinCodeModal } from '../../../redux/actions/applicationActions'; import { closePinCodeModal, login, logoutDone } from '../../../redux/actions/applicationActions';
import { getExistUser, setExistUser, getUserDataWithUsername } from '../../../realm/realm'; import {
getExistUser,
setExistUser,
getUserDataWithUsername,
removeAllUserData,
removePinCode,
setAuthStatus,
} from '../../../realm/realm';
import { import {
updateCurrentAccount, updateCurrentAccount,
setPinCode as savePinCode, setPinCode as savePinCode,
removeOtherAccount,
} from '../../../redux/actions/accountAction'; } from '../../../redux/actions/accountAction';
import { getUser } from '../../../providers/steem/dsteem'; import { getUser } from '../../../providers/steem/dsteem';
@ -112,11 +120,14 @@ class PinCodeContainer extends Component {
resolve(); resolve();
}) })
.catch((err) => { .catch((err) => {
Alert.alert(intl.formatMessage({ Alert.alert(
intl.formatMessage({
id: 'alert.warning', id: 'alert.warning',
}), intl.formatMessage({ }),
intl.formatMessage({
id: err.message, id: err.message,
})); }),
);
reject(err); reject(err);
}); });
} }
@ -154,12 +165,7 @@ class PinCodeContainer extends Component {
_verifyPinCode = pin => new Promise((resolve, reject) => { _verifyPinCode = pin => new Promise((resolve, reject) => {
const { const {
currentAccount, currentAccount, dispatch, accessToken, navigateTo, navigation, intl,
dispatch,
accessToken,
navigateTo,
navigation,
intl,
} = this.props; } = this.props;
// If the user is exist, we are just checking to pin and navigating to home screen // 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) => { .catch((err) => {
Alert.alert(intl.formatMessage({ Alert.alert(
intl.formatMessage({
id: 'alert.warning', id: 'alert.warning',
}), intl.formatMessage({ }),
intl.formatMessage({
id: err.message, id: err.message,
})); }),
);
reject(err); reject(err);
}); });
}); });
@ -264,6 +273,42 @@ class PinCodeContainer extends Component {
}, 1000); }, 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() { render() {
const { currentAccount, intl, isReset } = this.props; const { currentAccount, intl, isReset } = this.props;
const { informationText, isExistUser } = this.state; const { informationText, isExistUser } = this.state;
@ -274,6 +319,7 @@ class PinCodeContainer extends Component {
showForgotButton={isExistUser} showForgotButton={isExistUser}
username={currentAccount.name} username={currentAccount.name}
intl={intl} intl={intl}
handleForgotButton={() => this._handleForgotButton()}
{...this.props} {...this.props}
/> />
); );
@ -283,6 +329,7 @@ class PinCodeContainer extends Component {
const mapStateToProps = state => ({ const mapStateToProps = state => ({
currentAccount: state.account.currentAccount, currentAccount: state.account.currentAccount,
applicationPinCode: state.account.pin, applicationPinCode: state.account.pin,
otherAccounts: state.account.otherAccounts,
}); });
export default injectIntl(connect(mapStateToProps)(PinCodeContainer)); export default injectIntl(connect(mapStateToProps)(PinCodeContainer));

View File

@ -51,7 +51,7 @@ class PinCodeScreen extends PureComponent {
render() { render() {
const { const {
informationText, showForgotButton, username, intl, informationText, showForgotButton, username, intl, handleForgotButton,
} = this.props; } = this.props;
const { pin, loading } = this.state; const { pin, loading } = this.state;
@ -73,7 +73,7 @@ class PinCodeScreen extends PureComponent {
<NumericKeyboard onPress={this._handleKeyboardOnPress} /> <NumericKeyboard onPress={this._handleKeyboardOnPress} />
</View> </View>
{showForgotButton ? ( {showForgotButton ? (
<TouchableOpacity style={styles.forgotButtonView}> <TouchableOpacity onPress={() => handleForgotButton()} style={styles.forgotButtonView}>
<Text style={styles.forgotButtonText}> <Text style={styles.forgotButtonText}>
{intl.formatMessage({ {intl.formatMessage({
id: 'pincode.forgot_text', id: 'pincode.forgot_text',