Skip to content

Instantly share code, notes, and snippets.

@icfantv
Created February 25, 2016 00:53
Show Gist options
  • Save icfantv/ecf185f55e0a28ed624a to your computer and use it in GitHub Desktop.
Save icfantv/ecf185f55e0a28ed624a to your computer and use it in GitHub Desktop.
Angular HTTP Interceptors
var app = angular.module('app', []);
app.config(['$httpProvider', ConfigureInjectors]);
function ConfigureInjectors($httpProvider) {
$httpProvider.interceptors.push('InterceptorService');
}
app.service('InterceptorService', ['$cookie', InterceptorService]);
function InterceptorService($cookie) {
this.request = function(config) {
}
this.requestError = function(rejection) {
}
this.response = function(response) {
// set your auth token on the $cookie service here.
}
this.responseError = function(rejection) {
}
}
@mu-arch
Copy link

mu-arch commented Feb 25, 2016

os.factory('sessionManager', 
  function () {

  var auth = {};

  return {

    create: function (access_token, token_type, client, expiry, uid) {
      auth = {
        access_token: access_token,
        token_type: token_type,
        client: client,
        expiry: expiry,
        uid: uid,
      };
      return auth;
    },

    destroy: function () {
      auth = {
        access_token: null,
        token_type: null,
        client: null,
        expiry: null,
        uid: null,
      };
      return auth;
    },

    request: function (package_headers) {

      package_headers.headers['access-token'] = auth.access_token;
      package_headers.headers['token-type'] =  auth.token_type;
      package_headers.headers['client'] =  auth.client;
      package_headers.headers['expiry'] =  auth.expiry;
      package_headers.headers['uid'] = auth.uid;

      return package_headers;
    },
  }

});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment