mirror of
https://github.com/ecency/ecency-mobile.git
synced 2025-01-07 06:32:02 +03:00
refreshing access token on every new session, account switch and pin enter
This commit is contained in:
parent
b6036afe07
commit
5a42deadb5
@ -7,13 +7,18 @@ import { updateCurrentAccount } from '../../../redux/actions/accountAction';
|
||||
import { isRenderRequired } from '../../../redux/actions/applicationActions';
|
||||
|
||||
import { getUserDataWithUsername } from '../../../realm/realm';
|
||||
import { migrateToMasterKeyWithAccessToken, switchAccount } from '../../../providers/hive/auth';
|
||||
import {
|
||||
migrateToMasterKeyWithAccessToken,
|
||||
refreshSCToken,
|
||||
switchAccount,
|
||||
} from '../../../providers/hive/auth';
|
||||
|
||||
import AccountsBottomSheet from '../view/accountsBottomSheetView';
|
||||
import { toggleAccountsBottomSheet } from '../../../redux/actions/uiAction';
|
||||
|
||||
//Constants
|
||||
import AUTH_TYPE from '../../../constants/authType';
|
||||
import { getDigitPinCode } from '../../../providers/hive/dhive';
|
||||
|
||||
const AccountsBottomSheetContainer = ({ navigation }) => {
|
||||
const dispatch = useDispatch();
|
||||
@ -76,6 +81,13 @@ const AccountsBottomSheetContainer = ({ navigation }) => {
|
||||
_currentAccount = await migrateToMasterKeyWithAccessToken(_currentAccount, pinHash);
|
||||
}
|
||||
|
||||
//refresh access token
|
||||
const encryptedAccessToken = await refreshSCToken(
|
||||
_currentAccount.local,
|
||||
getDigitPinCode(pinHash),
|
||||
);
|
||||
_currentAccount.local.accessToken = encryptedAccessToken;
|
||||
|
||||
dispatch(updateCurrentAccount(_currentAccount));
|
||||
};
|
||||
|
||||
|
@ -642,18 +642,19 @@ export const getNodes = () => serverList.get().then((resp) => resp.data.hived ||
|
||||
* @param code refresh token
|
||||
* @returns scToken (includes accessToken as property)
|
||||
*/
|
||||
export const getSCAccessToken = (code) =>
|
||||
new Promise((resolve, reject) => {
|
||||
ecencyApi
|
||||
.post('/auth-api/hs-token-refresh', {
|
||||
code,
|
||||
})
|
||||
.then((resp) => resolve(resp.data))
|
||||
.catch((e) => {
|
||||
bugsnag.notify(e);
|
||||
reject(e);
|
||||
});
|
||||
});
|
||||
export const getSCAccessToken = async (code:string) => {
|
||||
try{
|
||||
const response = await ecencyApi.post('/auth-api/hs-token-refresh', {
|
||||
code,
|
||||
})
|
||||
return response.data;
|
||||
}catch(error){
|
||||
console.warn("failed to refresh token")
|
||||
bugsnag.notify(error);
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
@ -316,15 +316,17 @@ export const refreshSCToken = async (userData, pinCode) => {
|
||||
const scAccount = await getSCAccount(userData.username);
|
||||
const now = new Date();
|
||||
const expireDate = new Date(scAccount.expireDate);
|
||||
if (now >= expireDate) {
|
||||
const newSCAccountData = await getSCAccessToken(scAccount.refreshToken);
|
||||
await setSCAccount(newSCAccountData);
|
||||
const accessToken = newSCAccountData.access_token;
|
||||
await updateUserData({
|
||||
...userData,
|
||||
accessToken: encryptKey(accessToken, pinCode),
|
||||
});
|
||||
}
|
||||
|
||||
const newSCAccountData = await getSCAccessToken(scAccount.refreshToken);
|
||||
|
||||
await setSCAccount(newSCAccountData);
|
||||
const accessToken = newSCAccountData.access_token;
|
||||
const encryptedAccessToken = encryptKey(accessToken, pinCode);
|
||||
await updateUserData({
|
||||
...userData,
|
||||
accessToken: encryptedAccessToken,
|
||||
});
|
||||
return encryptedAccessToken;
|
||||
};
|
||||
|
||||
export const switchAccount = (username) =>
|
||||
|
@ -41,8 +41,12 @@ import {
|
||||
getVersionForWelcomeModal,
|
||||
setVersionForWelcomeModal,
|
||||
} from '../../../realm/realm';
|
||||
import { getUser, getPost } from '../../../providers/hive/dhive';
|
||||
import { migrateToMasterKeyWithAccessToken, switchAccount } from '../../../providers/hive/auth';
|
||||
import { getUser, getPost, getDigitPinCode } from '../../../providers/hive/dhive';
|
||||
import {
|
||||
migrateToMasterKeyWithAccessToken,
|
||||
refreshSCToken,
|
||||
switchAccount,
|
||||
} from '../../../providers/hive/auth';
|
||||
import {
|
||||
setPushToken,
|
||||
markActivityAsRead,
|
||||
@ -635,6 +639,30 @@ class ApplicationContainer extends Component {
|
||||
return null;
|
||||
};
|
||||
|
||||
_refreshAccessToken = async (currentAccount) => {
|
||||
const { pinCode, isPinCodeOpen, dispatch } = this.props;
|
||||
|
||||
if (isPinCodeOpen) {
|
||||
return currentAccount;
|
||||
}
|
||||
|
||||
try {
|
||||
const userData = currentAccount.local;
|
||||
const encryptedAccessToken = await refreshSCToken(userData, getDigitPinCode(pinCode));
|
||||
|
||||
return {
|
||||
...currentAccount,
|
||||
local: {
|
||||
...userData,
|
||||
accessToken: encryptedAccessToken,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.warn('Failed to refresh access token', error);
|
||||
return currentAccount;
|
||||
}
|
||||
};
|
||||
|
||||
_fetchUserDataFromDsteem = async (realmObject) => {
|
||||
const { dispatch, intl, pinCode } = this.props;
|
||||
|
||||
@ -647,6 +675,9 @@ class ApplicationContainer extends Component {
|
||||
accountData = await migrateToMasterKeyWithAccessToken(accountData, pinCode);
|
||||
}
|
||||
|
||||
//refresh access token
|
||||
accountData = await this._refreshAccessToken(accountData);
|
||||
|
||||
dispatch(updateCurrentAccount(accountData));
|
||||
|
||||
this._connectNotificationServer(accountData.name);
|
||||
@ -820,6 +851,9 @@ class ApplicationContainer extends Component {
|
||||
_currentAccount = await migrateToMasterKeyWithAccessToken(_currentAccount, pinCode);
|
||||
}
|
||||
|
||||
//update refresh token
|
||||
_currentAccount = await this._refreshAccessToken(_currentAccount);
|
||||
|
||||
dispatch(updateCurrentAccount(_currentAccount));
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user