Here's the core of the code I'm using. Assuming you have all the properties defined etc. You'll also need an AuthenticationView defined in scope somewhere too.
// assuming you have a AuthenticationView defined
// for signing into a portal instance
AuthenticationView {
id: authView
anchors.fill: parent
}
// this goes in your main app component
Component.onCompleted: {
// we are using secure storage to store a refresh token that is attempted to be retrieved
// if the secureStorage.getContent("appRefreshToken") returns an empty string
// i.e. the refresh token has never been stored
// then don't pass it into the credential which will prompt for sign in (using the authView defined above)
// if the refresh token is successfully retrieved from storage pass it into the credential which will use it to generate a fresh token
// create oAuth info specifying that we are requiring a named user sign in and a clientid unique to this app
let oAuthClientInfo = ArcGISRuntimeEnvironment.createObject("OAuthClientInfo", {
oAuthMode: Enums.OAuthModeUser,
clientId: app.clientId
});
let credential = ArcGISRuntimeEnvironment.createObject("Credential", { oAuthClientInfo: oAuthClientInfo });
// attempt to get the refresh token from storage
// so it can be used to generate an access token rather than forcing user to log in again
const refreshToken = secureStorage.getContent("appRefreshToken");
// if we got a refresh token apply it to the credential
// if we did not get one, returns empty string if the key is not found in storage
// don't set the credential refresh token which will prompt for sign in via AuthenticationView
// when loading the portal
if(refreshToken !== "") {
credential.oAuthRefreshToken = refreshToken;
credential.tokenServiceUrl = "%1/sharing/rest/oauth2/token".arg(app.portalUrl);
}
app.portal = ArcGISRuntimeEnvironment.createObject("Portal", {url: app.portalUrl, credential: credential});
app.portal.load();
app.portal.onLoadStatusChanged.connect(()=>{
if (app.portal.loadStatus === Enums.LoadStatusFailedToLoad) {
console.log(app.portal.error.message);
// assume our refresh token was invalidated so set to empty which prompts for log in
app.portal.credential.oAuthRefreshToken = "";
app.portal.retryLoad();
}
else if(app.portal.loadStatus === Enums.LoadStatusLoaded) {
// store the refresh token and token service url for retrieval next time the app is started
// so the user isn't prompted to log in again
secureStorage.setContent("appRefreshToken", app.portal.credential.oAuthRefreshToken);
secureStorage.setContent("tokenServiceUrl", app.portal.credential.tokenServiceUrl);
}
})
}
Good Luck! :collision: