タケユー・ウェブ日報

Ruby on Rails や Flutter といったWeb・モバイルアプリ技術を武器にお客様のビジネス立ち上げを支援する、タケユー・ウェブ株式会社の技術ブログです。

AngularJS cookieにセットしたAPIトークンをHTTPヘッダーに面倒のないように入れたい

Interceptorsを使う方向で落ち着いた。

angular.module('app').config(['$httpProvider', ($httpProvider)->
    $httpProvider.interceptors.push 'httpRequestInterceptor'
])
angular.module('app').factory('httpRequestInterceptor', ['ipCookie', (ipCookie)->
    {
        request: ($config)->
            $config.headers['X-Token'] = ipCookie('authentication.token')
            return $config
    }
])

ipCookie(angular-cookie)を使っているのは好みの問題。$cookiesでも同じと思います。