So as I've learned from the ESRI support this is not possible to do the enterprise logins. So time for hacking again....
If anyone is interested, I've ended up copying the session storage entry that IdentityManager genarates into the local storage after each login. This handles also agol/any portal.
It's not the neatest piece of code but serves the purpose.
changeOAuthStorage() {
// REMOVE ANY ESRIJSAPIOAUTH entries in the session or local storages
const key = 'esriJSAPIOAuth';
if (window && window.sessionStorage) {
const oauth = window.sessionStorage.getItem(key);
if (!oauth) {
return;
}
const oauthObj = JSON.parse(oauth);
if (window.localStorage) {
const localStorageoAuths = window.localStorage.getItem(key);
if (!localStorageoAuths) {
window.localStorage.setItem(key, JSON.stringify(oauthObj));
window.sessionStorage.removeItem(key);
return;
}
const lsAuth = JSON.parse(localStorageoAuths);
lsAuth['/'] = {...lsAuth['/'], ...oauthObj['/']};
window.localStorage.setItem(key, JSON.stringify(lsAuth));
window.sessionStorage.removeItem(key);
}
}
}
I've also noticed that IdentityManager 3.x has different inner workings to 4.x as it's not storing any serialized state into session nor the local storage. Has the capability of restoring the state even if accessing the internal portal externally via azure application proxy. 4.x cannot handle it.