From eff7ebc8b46f952c1eef5fa7c4f96d2c0912a4cf Mon Sep 17 00:00:00 2001 From: Mustafa Buyukcelebi Date: Wed, 9 Jan 2019 11:45:09 +0300 Subject: [PATCH] Fixed new login and pin issue --- src/providers/steem/auth.js | 72 +++++++++---------- src/realm/realm.js | 30 ++++++++ .../container/applicationContainer.js | 35 ++++++++- 3 files changed, 94 insertions(+), 43 deletions(-) diff --git a/src/providers/steem/auth.js b/src/providers/steem/auth.js index ad1879e0b..a08080a4b 100644 --- a/src/providers/steem/auth.js +++ b/src/providers/steem/auth.js @@ -142,31 +142,7 @@ export const setUserDataWithPinCode = async (data) => { const result = getUserDataWithUsername(data.username); const userData = result[0]; - const privateKeys = getPrivateKeys(userData.username, data.password); - const updatedUserData = { - username: userData.username, - authType: userData.authType, - accessToken: - userData.authType === 'steemConnect' - ? encryptKey(data.accessToken, data.pinCode) - : '', - masterKey: - userData.authType === 'masterKey' - ? encryptKey(data.password, data.pinCode) - : '', - postingKey: - userData.authType === 'masterKey' || userData.authType === 'postingKey' - ? encryptKey(privateKeys.postingKey.toString(), data.pinCode) - : '', - activeKey: - userData.authType === 'masterKey' || userData.authType === 'activeKey' - ? encryptKey(privateKeys.activeKey.toString(), data.pinCode) - : '', - memoKey: - userData.authType === 'masterKey' || userData.authType === 'memoKey' - ? encryptKey(privateKeys.memoKey.toString(), data.pinCode) - : '', - }; + const updatedUserData = getUpdatedUserData(userData, data); await setPinCode(data.pinCode); await updateUserData(updatedUserData); @@ -178,29 +154,17 @@ export const setUserDataWithPinCode = async (data) => { }; export const updatePinCode = async (data) => { - let password = null; - let accessToken = null; try { await setPinCode(data.pinCode); const users = await getUserData(); if (users.length > 0) { users.forEach(async (userData) => { if (userData.authType === 'masterKey') { - password = decryptKey(userData.masterKey, data.oldPinCode); + data.password = decryptKey(userData.masterKey, data.oldPinCode); } else if (userData.authType === 'steemConnect') { - accessToken = decryptKey(userData.accessToken, data.oldPinCode); + data.accessToken = decryptKey(userData.accessToken, data.oldPinCode); } - const privateKeys = getPrivateKeys(userData.username, password); - const updatedUserData = { - username: userData.username, - authType: userData.authType, - accessToken: - userData.authType === 'steemConnect' ? encryptKey(accessToken, data.pinCode) : '', - masterKey: userData.authType === 'masterKey' ? encryptKey(password, data.pinCode) : '', - postingKey: encryptKey(privateKeys.posting.toString(), data.pinCode), - activeKey: encryptKey(privateKeys.active.toString(), data.pinCode), - memoKey: encryptKey(privateKeys.memo.toString(), data.pinCode), - }; + const updatedUserData = getUpdatedUserData(userData, data); await updateUserData(updatedUserData); }); return true; @@ -276,6 +240,34 @@ const getPrivateKeys = (username, password) => { } }; +export const getUpdatedUserData = (userData, data) => { + const privateKeys = getPrivateKeys(userData.username, data.password); + return { + username: userData.username, + authType: userData.authType, + accessToken: + userData.authType === 'steemConnect' + ? encryptKey(data.accessToken, data.pinCode) + : '', + masterKey: + userData.authType === 'masterKey' + ? encryptKey(data.password, data.pinCode) + : '', + postingKey: + userData.authType === 'masterKey' || userData.authType === 'postingKey' + ? encryptKey(privateKeys.postingKey.toString(), data.pinCode) + : '', + activeKey: + userData.authType === 'masterKey' || userData.authType === 'activeKey' + ? encryptKey(privateKeys.activeKey.toString(), data.pinCode) + : '', + memoKey: + userData.authType === 'masterKey' || userData.authType === 'memoKey' + ? encryptKey(privateKeys.memoKey.toString(), data.pinCode) + : '', + }; +}; + const isLoggedInUser = (username) => { const result = getUserDataWithUsername(username); if (result.length > 0) { diff --git a/src/realm/realm.js b/src/realm/realm.js index cc480f33c..ee92ce230 100644 --- a/src/realm/realm.js +++ b/src/realm/realm.js @@ -306,6 +306,19 @@ export const setPinCode = pinCode => new Promise((resolve, reject) => { } }); +export const removePinCode = () => new Promise((resolve, reject) => { + try { + const auth = realm.objects(AUTH_SCHEMA); + + realm.write(() => { + auth[0].pinCode = ''; + resolve(auth[0]); + }); + } catch (error) { + reject(error); + } +}); + export const getPinCode = () => new Promise((resolve, reject) => { try { const auth = realm.objects(AUTH_SCHEMA); @@ -557,3 +570,20 @@ export const getSCAccount = username => new Promise((resolve, reject) => { reject(error); } }); + +export const removeSCAccount = username => new Promise((resolve, reject) => { + try { + const scAccount = realm.objects(SC_ACCOUNTS).filtered('username = $0', username); + + if (Array.from(scAccount).length > 0) { + realm.write(() => { + realm.delete(scAccount); + resolve(); + }); + } else { + reject(new Error('Could not remove selected user')); + } + } catch (error) { + reject(error); + } +}); diff --git a/src/screens/application/container/applicationContainer.js b/src/screens/application/container/applicationContainer.js index f77f8fad5..4edd8cd63 100644 --- a/src/screens/application/container/applicationContainer.js +++ b/src/screens/application/container/applicationContainer.js @@ -21,6 +21,10 @@ import { removeUserData, setPushTokenSaved, getUserDataWithUsername, + removePinCode, + setAuthStatus, + removeSCAccount, + setExistUser, } from '../../../realm/realm'; import { getUser } from '../../../providers/steem/dsteem'; import { setPushToken } from '../../../providers/esteem/esteem'; @@ -103,9 +107,25 @@ class ApplicationContainer extends Component { if (userData.length > 0) { realmData = userData; userData.forEach((accountData, index) => { - if (!accountData.accessToken && !accountData.masterKey) { + if ( + !accountData.accessToken + && !accountData.masterKey + && !accountData.postingKey + && !accountData.activeKey + && !accountData.memoKey + ) { realmData.splice(index, 1); - removeUserData(accountData); + if (realmData.length <= 0) { + dispatch(login(false)); + dispatch(logoutDone()); + removePinCode(); + setAuthStatus({ isLoggedIn: false }); + setExistUser(false); + if (accountData.authType === 'steemConnect') { + removeSCAccount(accountData.username); + } + } + removeUserData(accountData.username); } else { dispatch(addOtherAccount({ username: accountData.username })); } @@ -198,7 +218,9 @@ class ApplicationContainer extends Component { }; _logout = () => { - const { otherAccounts, currentAccountUsername, dispatch } = this.props; + const { + otherAccounts, currentAccountUsername, currentAccountAuthType, dispatch, + } = this.props; removeUserData(currentAccountUsername) .then(() => { @@ -213,6 +235,12 @@ class ApplicationContainer extends Component { } else { dispatch(updateCurrentAccount({})); dispatch(login(false)); + removePinCode(); + setAuthStatus({ isLoggedIn: false }); + setExistUser(false); + if (currentAccountAuthType === 'steemConnect') { + removeSCAccount(currentAccountUsername); + } } dispatch(removeOtherAccount(currentAccountUsername)); @@ -263,6 +291,7 @@ const mapStateToProps = state => ({ currentAccountUsername: state.account.currentAccount.name, otherAccounts: state.account.otherAccounts, pinCode: state.account.pin, + currentAccountAuthType: state.account.currentAccount.local, }); export default connect(mapStateToProps)(ApplicationContainer);