Fixed new login and pin issue

This commit is contained in:
Mustafa Buyukcelebi 2019-01-09 11:45:09 +03:00
parent 96e5f2de79
commit eff7ebc8b4
3 changed files with 94 additions and 43 deletions

View File

@ -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) {

View File

@ -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);
}
});

View File

@ -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);