Merge pull request #2706 from ecency/nt/already-logged-fix

Nt/already logged fix
This commit is contained in:
Feruz M 2023-06-12 23:03:32 +03:00 committed by GitHub
commit 4d61457909
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 43 deletions

View File

@ -457,7 +457,8 @@ export const getUpdatedUserKeys = async (currentAccountData, data) => {
const isLoggedInUser = async (username) => {
const result = await getUserDataWithUsername(username);
if (result.length > 0) {
const scAccount = await getSCAccount(username);
if (result.length > 0 && !!scAccount) {
return true;
}
return false;

View File

@ -55,13 +55,12 @@ export const getUserDataWithUsername = async (username) => {
export const setUserData = async (userData) => {
try {
const account = await getUserDataWithUsername(userData.username);
const user = (await getItemFromStorage(USER_SCHEMA)) || [];
const users = (await getItemFromStorage(USER_SCHEMA)) || [];
//replace user data if exist already else add new
const filteredUsers = users.filter((account) => { account.username !== userData.username });
await setItemToStorage(USER_SCHEMA, [...filteredUsers, userData]);
if (account.length === 0) {
user.push(userData);
await setItemToStorage(USER_SCHEMA, user);
}
return userData;
} catch (error) {
return error;

View File

@ -361,9 +361,13 @@ class ApplicationContainer extends Component {
};
_refreshUnreadActivityCount = async () => {
const { dispatch } = this.props;
const unreadActivityCount = await getUnreadNotificationCount();
dispatch(updateUnreadActivityCount(unreadActivityCount));
const { dispatch, isLoggedIn } = this.props;
if(isLoggedIn){
const unreadActivityCount = await getUnreadNotificationCount();
dispatch(updateUnreadActivityCount(unreadActivityCount));
}
};
_getUserDataFromRealm = async () => {
@ -573,45 +577,53 @@ class ApplicationContainer extends Component {
repairUserAccountData(username, dispatch, intl, otherAccounts, pinCode);
};
_logout = (username) => {
_logout = async (username) => {
const { otherAccounts, dispatch, intl } = this.props;
removeUserData(username)
.then(async () => {
this._enableNotification(username, false);
try {
const response = await removeUserData(username)
// switch account if other account exist
const _otherAccounts = otherAccounts.filter((user) => user.username !== username);
if(response instanceof Error){
throw response;
}
if (_otherAccounts.length > 0) {
const targetAccount = _otherAccounts[0];
await this._switchAccount(targetAccount);
}
this._enableNotification(username, false);
// logut from app if no more other accounts
else {
dispatch(updateCurrentAccount({}));
dispatch(login(false));
removePinCode();
setAuthStatus({
isLoggedIn: false,
});
setExistUser(false);
dispatch(isPinCodeOpen(false));
dispatch(setEncryptedUnlockPin(encryptKey(Config.DEFAULT_KEU, Config.PIN_KEY)));
}
// switch account if other account exist
const _otherAccounts = otherAccounts.filter((user) => user.username !== username);
removeSCAccount(username);
dispatch(setFeedPosts([]));
dispatch(setInitPosts([]));
dispatch(removeOtherAccount(username));
dispatch(logoutDone());
})
.catch((err) => {
dispatch(logoutDone());
Alert.alert(intl.formatMessage({ id: 'alert.fail' }), err.message);
this._repairUserAccountData(username);
});
if (_otherAccounts.length > 0) {
const targetAccount = _otherAccounts[0];
await this._switchAccount(targetAccount);
}
// logut from app if no more other accounts
else {
dispatch(updateCurrentAccount({}));
dispatch(login(false));
removePinCode();
setAuthStatus({
isLoggedIn: false,
});
setExistUser(false);
dispatch(isPinCodeOpen(false));
dispatch(setEncryptedUnlockPin(encryptKey(Config.DEFAULT_KEU, Config.PIN_KEY)));
}
removeSCAccount(username);
dispatch(setFeedPosts([]));
dispatch(setInitPosts([]));
dispatch(removeOtherAccount(username));
dispatch(logoutDone());
}
catch(err) {
dispatch(logoutDone());
Alert.alert(intl.formatMessage({ id: 'alert.fail' }), err.message);
this._repairUserAccountData(username);
}
};
_enableNotification = async (username, isEnable, settings = null, accessToken = null) => {