David Poxon
David Poxon

Reputation: 2503

Pass cookie created by API back to API from Angular app

I have an API that creates a session cookie named 'web_api'. I would then like to access that cookie in my Angular app so that I can pass it back to the API for authorisation purposes. However, using $cookies.web_api and $cookieStore.get('web_api') yields undefined.

Is it possible to somehow pass this cookie back to my API from the Angular app?

My service code is as follows:

app.service('apiService', function($http, $q, $cookies, $cookieStore) {
    var host = 'https://127.0.0.1:12344/v1/'

    function test_cookie() {
        console.log($cookies.web_api);
        console.log($cookieStore.get('web_api'));
    }
});

Upvotes: 0

Views: 844

Answers (1)

David Poxon
David Poxon

Reputation: 2503

I set the the API endpoint to 'Access-Control-Allow-Credentials' = true and when I made the request I set withCredentials: true on the request, e.g.

function getUserId() {
    var idPath = 'userId'

    var request = $http({
        method: 'get',
        url: buildUrl(idPath),
        withCredentials: true
    });

    return(request.then(handleSuccess, handleError));
}

This resolved the issue.

This answer was based on on the documentation provided here: http://www.html5rocks.com/en/tutorials/cors/

Upvotes: 1

Related Questions