2014-01-06 31 views
3

拖延我遇到一個奇怪的問題與ngCookies服務。主要是,從我/api/auth/login/終端接收響應,與頭後:曲奇從響應更新在未來

Set-Cookie csrftoken=gxCld8gEga71MuQPQbjDujDBvR4HwPvu; expires=Sun, 28-Dec-2014 15:31:38 GMT; Max-Age=31449600; Path=/ 

$cookies['csrftoken']的時間不確定量後更新。這一事實已被記錄在docs

Only a simple Object is exposed and by adding or removing properties to/from this object, new cookies are created/deleted at the end of current $eval. 

知道了,我已經使出了使用$timeout,希望能夠推遲我的代碼評價上述$eval後。

結束了與以下(CoffeeScript的):

login =() -> 
    deferred = $q.defer() 

    $http.post('/api/accounts/login/', data) 
    .success((data) -> 
     args = arguments 
     $timeout(() -> 
      do_fancy_stuff() 
      deferred.resolve.apply(deferred, args) 
     ) 
     return 
    ).error(() -> 
     args = arguments 
     $timeout(() -> 
      deferred.reject.apply(deferred, args) 
     ) 
     return 
    ) 

    promise = deferred.promise 
    promise.success = promise.then 
    promise.error = promise.catch 
    promise 

但上面的代碼還是從問題受到影響。在$timeout開始後,曲奇用更新的響應方式的值更新。

添加console.log('cookies push', $browser.cookies().csrftoken, cookies.csrftoken);here(之後if聲明)。我已經結束了這樣的事情:

enter image description here

正如你所看到的,令牌是經過7和版畫等。

yhQqT6KOfSKYCNB3Ag4sEPllMgkLrVj1令牌來自於前一交易日。我正在測試註銷並進入應用程序,無需刷新頁面。該Setting X-CSRFToken在我do_fancy_stuff()功能(沒有異步的東西,只是裸$cookies['csrftoken']訪問)直接調用。

還需要使用$q提供$http般的承諾作爲返回值(就沒有$q如果甜餅做工精細)。

+0

我想我遇到了類似的問題。任何解決方案的進展? –

+0

@SethM,其實我已經做它,這樣我的'login'視圖返回'與令牌的值X-CSRFToken'頭。有了這個,可以直接在即將到來的請求的響應中設置默認的'$ http'頭部。請注意,我聲稱這是一個黑客而非解決方案。 –

回答

4

我選擇使用的解決方案基於找到的信息herehere。告訴我,$ cookie會嘗試每100ms刷新一次。因此,考慮到這一信息,您可以加入$超時需要使用響應的cookie的任何請求。

 $http.post(url, tokens) 
      .success(function(result, status, headers, config) { // sign in successful 
       $timeout(function() { /* do stuff with $cookies here. */ }, 100); 
      }) 
      .error(function(data, status, headers, config) { 
      }); 
+0

我試過40毫秒,它工作正常。你能解釋一下嗎? – bpceee