# Unable to set auth header using javascript client elasticsearch.js

**URL:** <https://discuss.elastic.co/t/unable-to-set-auth-header-using-javascript-client-elasticsearch-js/34372>\
**Category:** Elasticsearch\
**Created:** [November 12, 2015, 1:56am UTC](https://discuss.elastic.co/t/unable-to-set-auth-header-using-javascript-client-elasticsearch-js/34372 "2015-11-12T01:56:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SKumarMN](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/skumarmn/32/27537_2.png) [@SKumarMN](https://discuss.elastic.co/u/SKumarMN)\
**Post date:** [November 12, 2015, 1:56am UTC](https://discuss.elastic.co/t/unable-to-set-auth-header-using-javascript-client-elasticsearch-js/34372/1 "2015-11-12T01:56:11Z")

</div>

Hi,

I am using elasticsearch-angular.js client to connect from my UI built using angularjs to connect to elasticserach 1.4. Our es instance has a custom plugin with Base authentication to intercept every request to check if the incoming request has authorization header set. I am setting the auth param as described in the docs when instantiating the esFactory. However the auth headers are not getting set. Can you please let me know how to set auth headers from java script client for every request

var myServices = angular.module('myServices', ['elasticsearch']);

myServices.service('es', ['esFactory',  
function (esFactory, $http) {  
return esFactory({  
host: 'localhost:9200',  
auth: 'user:password'  
});  
}]);

---

<div class="post-metadata">

**Author:** ![SKumarMN](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/skumarmn/32/27537_2.png) [@SKumarMN](https://discuss.elastic.co/u/SKumarMN)\
**Post date:** [November 12, 2015, 2:55am UTC](https://discuss.elastic.co/t/unable-to-set-auth-header-using-javascript-client-elasticsearch-js/34372/2 "2015-11-12T02:55:59Z")

</div>

I have enabled log:trace param and i see the below error in chrome console

XMLHttpRequest cannot load [http://localhost:9200/\_cluster/state/cluster\_name%2Cnodes%2Cmaster\_node%2Cversion](http://localhost:9200/_cluster/state/cluster_name%2Cnodes%2Cmaster_node%2Cversion). Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.

I have set the cors in ES yml file as below

http.cors:  
enabled: true  
allow-origin: /https?://localhost(:[0-9]+)?/

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:39pm UTC](https://discuss.elastic.co/t/unable-to-set-auth-header-using-javascript-client-elasticsearch-js/34372/3 "2017-07-05T23:39:05Z")

</div>


