Authorization header not working

Hey guys

This is how I set my headers. I looked all over the internet but when I send it to the server it doesn’t recognize my Authorization header. How did you solve that problem?

createHeaders(headers:Headers) {
    this.storage.getItem('token')
    .then(
      (token) => { console.log('Bearer ' + token);
        headers.append('Authorization', 'Bearer ' + token); 
      },
      (error) => console.error(error)
    );
		  
    headers.append('AllowEntry', btoa(this.allow_entry)); 

    headers.append('Content-Type', 'application/json');

	}


  	/**
  	 * Post data to sevrer
  	 * @param any postData Data to post to the server
  	 * @param string url Url to post to
  	 */
	post(postData, url)
	{
		var headers = new Headers();
		this.createHeaders(headers);
        headers.set('Authorization', 'Bearer my-token'); 
        let options = new RequestOptions({ headers: headers });
		return this.http.post(ApiSettings.API_ENDPOINT+url, postData, options)
	    .toPromise()
	    .then(response => response.json())
	    .catch(this.handleError);
	}

Not sure if related, but once you start adding unusual headers, that triggers CORS.

I figured it out. I was doing:

 this.storage.getItem('token')
    .then(
      (token) => { console.log('Bearer ' + token);
        headers.append('Authorization', 'Bearer ' + token); 
      },
      (error) => console.error(error)
    );

Which created promise and my code continued toe execute and it didn’t wait for Authorization header to resolve. I need to put my code inside “then”