4

Ionic App Not Sending Authorization Header. The Header Values Are Simply Not There Even though the Authorization Header Is Listed Among The Allowed Headers. I Am Using 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
  • won't get much help if you don't show the code used to set the headers Commented Nov 3, 2015 at 22:59
  • is authToken.getToken() synchronous? Does it return a valid token? If it is asynchronous you have a problem Commented Nov 3, 2015 at 23:18
  • no its from localstorage Commented Nov 3, 2015 at 23:21
  • i mean yes its from localstorage Commented Nov 3, 2015 at 23:26

2 Answers 2

1

You can pass auth header before making $http call. I have used in this way and this worked for me for API auth,

$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 is helpful for you.

Sign up to request clarification or add additional context in comments.

Comments

0

I had the same issue. The only way I figured out to solve it was by adding:

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

to my ionic.project file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.