سؤال

I am following David Mosher's excellent frontend workflow tutorial, building an app with lineman.js, an angularjs frontend and a laravel backend (http://www.youtube.com/watch?v=fSAgFxjFSqY).

I am manually bootstrapping the app so that I can fetch the csrf token from the laravel backend and inject it as a constant before bootstrapping the angularjs app.

My bootstrap.js file:

(function() {
  var $injector = angular.injector(['ng']);
    $injector.invoke(function($http, $rootScope) {
      $rootScope.$apply(function() {
        $http.get("/api/auth/csrf_token").then(function(response) {
          angular.module("app").constant("CSRF_TOKEN", response.csrf_token);
          angular.bootstrap(document, ['app']);
        });
      });
    });
  })();

I am getting the csrf_token from laravel in my laravel routes.php file like this:

Route::get('/api/auth/csrf_token', function() {
  return Response::json(array('csrf_token' => csrf_token()));
});

I can see in Chrome Dev that I am fetching the csrf token from the backend:

XHR finished loading: "http://localhost:8000/api/auth/csrf_token". 
Network/Preview (->csrf_token): csrf_token: "BsMDDJ8ZjESpvdBLWBLz5ORM5LNXOsl3gx5LBcj5"

Problem: I cannot find the CSRF_TOKEN constant in my angular application.

Question: Is there an error in my definition of the CSRF_TOKEN constant or is there any other error in my bootstrapping code?

Any help would be much appreciated.

هل كانت مفيدة؟

المحلول

The CSRF_TOKEN is stored in 'data.csrf_token', not 'csrf_token'. I replaced

angular.module("app").constant("CSRF_TOKEN", response.csrf_token); 

with

angular.module("app").constant("CSRF_TOKEN", response.data.csrf_token); 

in the bootstrap.js file. Now it works.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top