Ionic - $ http.get () does not send authorization header

Ionic App Submit authorization header. Header values ​​are simply missing, although the authorization header is included in the list of allowed headers. I use NodeJS Express and MongoDB as a backend. Here are my CORS headers.

    <code>
res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods',
      'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Expose-Headers',
      'X-Authorization-Token, Authorization');
    res.header('Access-Control-Allow-Headers',
      'Origin, X-Requested-With, Content-Type, Accept, Authorization'
    );

   And Here Is My AuthInterceptor


    return {
      request: function(config) {
        var token = authToken.getToken();

        if (token) {
          config.headers.Authorization = 'Bearer ' + token;

        }
        return config;
      },
      response: function(response) {
        return response;
      }
    };

My $http

        return $http(options).success(function(response) {
          self.currUser = response.profile;
          if (_.contains(response.profile.roles, 'admin') ||
            !((_.contains(response.profile.roles, 'lecturer')) || (_.contains(
              response.profile.roles, 'evaluator')))) {
            $state.go('admin');
          }
        })
</code> 
+4
source share
2 answers

I had the same problem. The only way I decided to solve is to add:

"proxies": [
{
  "path": "/<your local url>",
  "proxyUrl": "http://<remote_url>"
}
],

to the ionic.project file.

0
source

You can pass the auth header before calling $ http. I used this way and it worked for me for the auth API,

$http.defaults.headers.get = { 'x-username' User.email,
                           'x-token':authToken.getToken(), 
                           'Content-Type':'application/json'};

$http.get(URL+'/api).then(function(res){console.log('success')});

Hope this helps you.

+1
source

Source: https://habr.com/ru/post/1614359/


All Articles