Refactored auth functions

This commit is contained in:
Mustafa Buyukcelebi 2018-12-21 15:56:45 +03:00
parent e311c7ca52
commit fbfa0622d5

View File

@ -13,9 +13,7 @@ import {
import { encryptKey, decryptKey } from '../../utils/crypto';
import steemConnect from './steemConnectAPI';
export const Login = (username, password) => {
let publicKeys;
let privateKeys;
export const Login = async (username, password) => {
const resultKeys = {
active: null,
memo: null,
@ -24,17 +22,17 @@ export const Login = (username, password) => {
};
let loginFlag = false;
let avatar = '';
return new Promise((resolve, reject) => {
// Get user account data from STEEM Blockchain
getUser(username)
.then((account) => {
const account = await getUser(username);
if (!account) {
return (new Error('Invalid credentials, please check and try again'));
}
if (isLoggedInUser(username)) {
reject(new Error('You are already logged in, please try to add another account'));
return (new Error('You are already logged in, please try to add another account'));
}
// Public keys of user
publicKeys = {
const publicKeys = {
active: account.active.key_auths.map(x => x[0]),
memo: account.memo_key,
owner: account.owner.key_auths.map(x => x[0]),
@ -42,7 +40,7 @@ export const Login = (username, password) => {
};
// Set private keys of user
privateKeys = getPrivateKeys(username, password);
const privateKeys = getPrivateKeys(username, password);
// Check all keys
Object.keys(publicKeys).map((pubKey) => {
@ -51,13 +49,12 @@ export const Login = (username, password) => {
resultKeys[pubKey] = publicKeys[pubKey];
}
});
let jsonMetadata;
try {
jsonMetadata = JSON.parse(account.json_metadata) || '';
} catch (err) {
jsonMetadata = '';
// TODO: handle wrong json format properly
// reject(new Error(err));
}
if (Object.keys(jsonMetadata).length !== 0) {
avatar = jsonMetadata.profile.profile_image || '';
@ -77,22 +74,11 @@ export const Login = (username, password) => {
account.local = userData;
// Save user data to Realm DB
setUserData(userData)
.then(() => {
resolve({ ...account, password });
updateCurrentUsername(account.name);
})
.catch(() => {
reject(new Error('Invalid credentials, please check and try again'));
});
} else {
reject(new Error('Invalid credentials, please check and try again'));
await setUserData(userData);
await updateCurrentUsername(account.name);
return ({ ...account, password });
}
})
.catch(() => {
reject(new Error('Invalid credentials, please check and try again'));
});
});
return (new Error('Invalid credentials, please check and try again'));
};
export const loginWithSC2 = async (accessToken) => {
@ -120,26 +106,15 @@ export const loginWithSC2 = async (accessToken) => {
accessToken: '',
};
const authData = {
isLoggedIn: true,
currentUsername: account.account.name,
};
if (isLoggedInUser(account.account.name)) {
reject(new Error('You are already logged in, please try to add another account'));
}
setAuthStatus(authData)
.then(() => {
setUserData(userData)
.then(() => {
account.account.username = account.account.name;
resolve({ ...account.account, accessToken });
updateCurrentUsername(account.account.name);
})
.catch((error) => {
reject(error);
});
resolve({ ...account.account, accessToken });
})
.catch((error) => {
reject(error);
@ -147,89 +122,52 @@ export const loginWithSC2 = async (accessToken) => {
});
};
export const setUserDataWithPinCode = data => new Promise((resolve, reject) => {
let updatedUserData;
export const setUserDataWithPinCode = async (data) => {
const result = getUserDataWithUsername(data.username);
const userData = result[0];
const privateKeys = getPrivateKeys(userData.username, data.password);
if (userData.authType === 'masterKey') {
updatedUserData = {
const updatedUserData = {
username: userData.username,
authType: 'masterKey',
masterKey: encryptKey(data.password, data.pinCode),
authType: userData.authType,
accessToken: userData.authType === 'steemConnect' ? encryptKey(data.accessToken, data.pinCode) : '',
masterKey: userData.authType === 'masterKey' ? encryptKey(data.password, data.pinCode) : '',
postingKey: encryptKey(privateKeys.posting.toString(), data.pinCode),
activeKey: encryptKey(privateKeys.active.toString(), data.pinCode),
memoKey: encryptKey(privateKeys.memo.toString(), data.pinCode),
};
} else if (userData.authType === 'steemConnect') {
updatedUserData = {
username: userData.username,
authType: 'steemConnect',
accessToken: encryptKey(data.accessToken, data.pinCode),
masterKey: '',
postingKey: encryptKey(privateKeys.posting.toString(), data.pinCode),
activeKey: encryptKey(privateKeys.active.toString(), data.pinCode),
memoKey: encryptKey(privateKeys.memo.toString(), data.pinCode),
};
}
updateUserData(updatedUserData)
.then((response) => {
.then(async (response) => {
const authData = {
isLoggedIn: true,
currentUsername: userData.username,
};
setAuthStatus(authData)
.then(() => {
await setAuthStatus(authData);
const encriptedPinCode = encryptKey(data.pinCode, 'pin-code');
setPinCode(encriptedPinCode)
.then(() => {
resolve(response);
await setPinCode(encriptedPinCode);
return (response);
})
.catch((error) => {
reject(error);
});
})
.catch((error) => {
reject(error);
});
})
.catch((err) => {
reject(err);
});
});
.catch(err => (err));
};
export const updatePinCode = async (data) => {
let updatedUserData;
const users = await getUserData();
if (users.length > 0) {
users.forEach(async (userData) => {
const password = decryptKey(userData.masterKey, data.oldPinCode);
const privateKeys = getPrivateKeys(userData.username, password);
if (userData.authType === 'masterKey') {
updatedUserData = {
const updatedUserData = {
username: userData.username,
authType: 'masterKey',
masterKey: encryptKey(password, data.pinCode),
authType: userData.authType,
accessToken: userData.authType === 'steemConnect' ? encryptKey(data.accessToken, data.pinCode) : '',
masterKey: userData.authType === 'masterKey' ? encryptKey(data.password, data.pinCode) : '',
postingKey: encryptKey(privateKeys.posting.toString(), data.pinCode),
activeKey: encryptKey(privateKeys.active.toString(), data.pinCode),
memoKey: encryptKey(privateKeys.memo.toString(), data.pinCode),
};
} else if (userData.authType === 'steemConnect') {
updatedUserData = {
username: userData.username,
authType: 'steemConnect',
accessToken: encryptKey(data.accessToken, data.pinCode),
masterKey: '',
postingKey: encryptKey(privateKeys.posting.toString(), data.pinCode),
activeKey: encryptKey(privateKeys.active.toString(), data.pinCode),
memoKey: encryptKey(privateKeys.memo.toString(), data.pinCode),
};
}
const response = await updateUserData(updatedUserData);
const authData = {
@ -292,8 +230,6 @@ export const verifyPinCode = async (data) => {
}
}
}
return new Promise((resolve, reject) => {
if (loginFlag) {
const authData = {
isLoggedIn: true,
@ -306,18 +242,10 @@ export const verifyPinCode = async (data) => {
activeKey: decryptKey(userData.activeKey, data.pinCode),
memoKey: decryptKey(userData.memoKey, data.pinCode),
};
setAuthStatus(authData)
.then(() => {
resolve(response);
})
.catch(() => {
// TODO: create function for throw error
reject(new Error('Unknown error, please contact to eSteem.'));
});
} else {
reject(new Error('Invalid pin code, please check and try again'));
await setAuthStatus(authData);
return (response);
}
});
return (new Error('Invalid pin code, please check and try again'));
};
export const switchAccount = username => new Promise((resolve, reject) => {