mirror of
https://github.com/ecency/ecency-mobile.git
synced 2024-12-22 21:01:31 +03:00
Refactored auth functions
This commit is contained in:
parent
e311c7ca52
commit
fbfa0622d5
@ -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,75 +22,63 @@ export const Login = (username, password) => {
|
||||
};
|
||||
let loginFlag = false;
|
||||
let avatar = '';
|
||||
// Get user account data from STEEM Blockchain
|
||||
const account = await getUser(username);
|
||||
if (!account) {
|
||||
return (new Error('Invalid credentials, please check and try again'));
|
||||
}
|
||||
if (isLoggedInUser(username)) {
|
||||
return (new Error('You are already logged in, please try to add another account'));
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// Get user account data from STEEM Blockchain
|
||||
getUser(username)
|
||||
.then((account) => {
|
||||
if (isLoggedInUser(username)) {
|
||||
reject(new Error('You are already logged in, please try to add another account'));
|
||||
}
|
||||
// Public keys of user
|
||||
const publicKeys = {
|
||||
active: account.active.key_auths.map(x => x[0]),
|
||||
memo: account.memo_key,
|
||||
owner: account.owner.key_auths.map(x => x[0]),
|
||||
posting: account.posting.key_auths.map(x => x[0]),
|
||||
};
|
||||
|
||||
// Public keys of user
|
||||
publicKeys = {
|
||||
active: account.active.key_auths.map(x => x[0]),
|
||||
memo: account.memo_key,
|
||||
owner: account.owner.key_auths.map(x => x[0]),
|
||||
posting: account.posting.key_auths.map(x => x[0]),
|
||||
};
|
||||
// Set private keys of user
|
||||
const privateKeys = getPrivateKeys(username, password);
|
||||
|
||||
// Set private keys of user
|
||||
privateKeys = getPrivateKeys(username, password);
|
||||
|
||||
// Check all keys
|
||||
Object.keys(publicKeys).map((pubKey) => {
|
||||
if (publicKeys[pubKey] === privateKeys[pubKey].createPublic().toString()) {
|
||||
loginFlag = true;
|
||||
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 || '';
|
||||
}
|
||||
if (loginFlag) {
|
||||
const userData = {
|
||||
username,
|
||||
avatar,
|
||||
authType: 'masterKey',
|
||||
masterKey: '',
|
||||
postingKey: '',
|
||||
activeKey: '',
|
||||
memoKey: '',
|
||||
accessToken: '',
|
||||
};
|
||||
|
||||
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'));
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
reject(new Error('Invalid credentials, please check and try again'));
|
||||
});
|
||||
// Check all keys
|
||||
Object.keys(publicKeys).map((pubKey) => {
|
||||
if (publicKeys[pubKey] === privateKeys[pubKey].createPublic().toString()) {
|
||||
loginFlag = true;
|
||||
resultKeys[pubKey] = publicKeys[pubKey];
|
||||
}
|
||||
});
|
||||
|
||||
let jsonMetadata;
|
||||
try {
|
||||
jsonMetadata = JSON.parse(account.json_metadata) || '';
|
||||
} catch (err) {
|
||||
jsonMetadata = '';
|
||||
}
|
||||
if (Object.keys(jsonMetadata).length !== 0) {
|
||||
avatar = jsonMetadata.profile.profile_image || '';
|
||||
}
|
||||
if (loginFlag) {
|
||||
const userData = {
|
||||
username,
|
||||
avatar,
|
||||
authType: 'masterKey',
|
||||
masterKey: '',
|
||||
postingKey: '',
|
||||
activeKey: '',
|
||||
memoKey: '',
|
||||
accessToken: '',
|
||||
};
|
||||
|
||||
account.local = userData;
|
||||
|
||||
// Save user data to Realm DB
|
||||
await setUserData(userData);
|
||||
await updateCurrentUsername(account.name);
|
||||
return ({ ...account, password });
|
||||
}
|
||||
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)
|
||||
setUserData(userData)
|
||||
.then(() => {
|
||||
setUserData(userData)
|
||||
.then(() => {
|
||||
account.account.username = account.account.name;
|
||||
resolve({ ...account.account, accessToken });
|
||||
updateCurrentUsername(account.account.name);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
account.account.username = account.account.name;
|
||||
updateCurrentUsername(account.account.name);
|
||||
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 = {
|
||||
username: userData.username,
|
||||
authType: 'masterKey',
|
||||
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 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: 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(() => {
|
||||
const encriptedPinCode = encryptKey(data.pinCode, 'pin-code');
|
||||
setPinCode(encriptedPinCode)
|
||||
.then(() => {
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
await setAuthStatus(authData);
|
||||
const encriptedPinCode = encryptKey(data.pinCode, 'pin-code');
|
||||
await setPinCode(encriptedPinCode);
|
||||
return (response);
|
||||
})
|
||||
.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 = {
|
||||
username: userData.username,
|
||||
authType: 'masterKey',
|
||||
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),
|
||||
};
|
||||
} 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 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: 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,32 +230,22 @@ export const verifyPinCode = async (data) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (loginFlag) {
|
||||
const authData = {
|
||||
isLoggedIn: true,
|
||||
currentUsername: data.username,
|
||||
};
|
||||
const response = {
|
||||
accessToken: decryptKey(userData.accessToken, data.pinCode),
|
||||
postingKey: decryptKey(userData.postingKey, data.pinCode),
|
||||
masterKey: decryptKey(userData.masterKey, data.pinCode),
|
||||
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'));
|
||||
}
|
||||
});
|
||||
if (loginFlag) {
|
||||
const authData = {
|
||||
isLoggedIn: true,
|
||||
currentUsername: data.username,
|
||||
};
|
||||
const response = {
|
||||
accessToken: decryptKey(userData.accessToken, data.pinCode),
|
||||
postingKey: decryptKey(userData.postingKey, data.pinCode),
|
||||
masterKey: decryptKey(userData.masterKey, data.pinCode),
|
||||
activeKey: decryptKey(userData.activeKey, data.pinCode),
|
||||
memoKey: decryptKey(userData.memoKey, data.pinCode),
|
||||
};
|
||||
await setAuthStatus(authData);
|
||||
return (response);
|
||||
}
|
||||
return (new Error('Invalid pin code, please check and try again'));
|
||||
};
|
||||
|
||||
export const switchAccount = username => new Promise((resolve, reject) => {
|
||||
|
Loading…
Reference in New Issue
Block a user