Select to view content in your preferred language

Initialise FeatureLayer with Token included api 4.28

284
1
02-04-2024 11:56 PM
Baral_lec
New Contributor

Hey everyone,

so in our APP we use a Portal login for Authentification, I add the used Code below. I noticed that when creating a new FeatureLayer a Request to /server/rest/services/03/xx/FeatureServer/2?f=json gets sent with response {"error":{"code":499,"message":"Token Required","details":[]}}, it automatically resends that request with token than it works. I'd like to savemyself all these requests and include the token on start.

Does anybody have a Idea how to accomplish that?

I tried setting up a interceptor with

    private readonly applyCredentialsInterceptor = (layerUrl: string):void => {
        esriConfig.request.interceptors?.push({
            urls: `${layerUrl}`,
            before(params) {
                params.requestOptions.withCredentials = true;
            },
        });
    };

before FeatureLayer Creation.

I added the token via the customParameter property while creating the featureLayer.

What seemed to work at first, was setting the token in the apiKey property while initializing the FeatureLayer, however after the token had to be refreshed, the client didn't change to the new token.

Any Idea would be appreciated, thanks 🙂


 

 private readonly portalLogin = (): void => {
        this.stores.mainStore.setUserLoginState('loading');
        const { appStore } = this.stores;
        const { config } = appStore;
        window.addEventListener('arcgis:auth:hash', (event) => {
            console.log('OAuth response:', event.detail);
            IdentityManager.setOAuthResponseHash(event.detail);
        });
        const oAuthInfo = new OAuthInfo({
            appId: this.stores.appStore.config.main.appRegistration.appId,
            portalUrl: this.stores.appStore.config.main.appRegistration.portalUrl,
        });

        this.portal = new Portal({ url: config.main.appRegistration.portalUrl });
        this.portal.load().then(() => {
            IdentityManager.registerOAuthInfos([oAuthInfo]);
            this.stores.mainStore.helperServices = this.portal.helperServices;
            this.stores.mainStore.portal = this.portal;
            IdentityManager.checkSignInStatus(`${oAuthInfo.portalUrl}/sharing`)
                .then((signInEvt: Credential) => {
                    this.stores.mainStore.user = signInEvt;
                    this.getGroupsTitleOfUser().then((groups: string[]) => {
                        this.stores.mainStore.userGroups = groups;
                        this.stores.mainStore.setUserLoginState('loaded');
                        console.log(`User ${signInEvt.userId} successful signed in. Assigned groups: ${this.stores.mainStore.userGroups}`, signInEvt);
                    });
                })
                .catch((error: Error) => {
                    console.log(error.message);
                    IdentityManager.getCredential(`${oAuthInfo.portalUrl}/sharing`, {
                        oAuthPopupConfirmation: false,
                    }).then((signInEvt: Credential) => {
                        this.stores.mainStore.user = signInEvt;
                        this.getGroupsTitleOfUser().then((groups: string[]) => {
                            this.stores.mainStore.userGroups = groups;
                            this.stores.mainStore.setUserLoginState('loaded');
                            console.log(`User ${signInEvt.userId} successful signed in. Assigned groups: ${this.stores.mainStore.userGroups}`, signInEvt);
                        });
                    });
                });
        });
    };
0 Kudos
1 Reply
JoelBennett
MVP Regular Contributor

Have you tried using the registerToken method of the IdentityManager module?  I've used it to do this same kind of thing.

For example:

esriId.registerToken({
	expires: tokenResponse.expires,
	server: "https://myServer/arcgis/rest/services",
	token: tokenResponse.token
});

 

0 Kudos