Q:

Arcgis: authentification via JS application

I am writing a program using JavaScript, which connects to a local ArcGIS server through ArcGIS REST API and loads the maps. 
The URL to get service is
let xmlhttp = new XMLHttpRequest();
xmlhttp.withCredentials = true;
let url = "http://domain/arcgis/rest/services/" + serviceName +"/MapServer/" + layer_id +  "/query?f=json&where=1%3D1&returnGeometry=false&outFields=*";

I can get the token through the API. The problem is, I can't set it in cookies, as the browser rejects programmatically writing another domain.
May someone suggest a workaround? 

A:

You can add &token=abcd1234 to your url, so you get :
let token = 'abcd1234';
let url = `http://domain/arcgis/rest/services/${serviceName}/MapServer/${layer_id}/query?f=json&where=1%3D1&returnGeometry=false&outFields=*&token=${token}`;

