title: Middleware type functionality of Laravel in AngularJS?
body: <p>I am new to AngularJS I have been recently deployed to a AngularJS project. Now at backend I use Laravel and I want to know if there is a way in Angular that I can create or use the same functionality what middleware does in Laravel.</p>
  
  <p>What I basically want is that I need if user is logged in then only he can move forward in the application to other pages.</p>
  
  <p>I have tried something but I don't feel it is correct.</p>
  
  <pre><code>app.controller('afterLogin', function ($scope, $http, $translate,$state,toastr,$auth) {
  
      var init = function () {
          var req = {
              method: 'GET',
              url: baseUrl + 'username',
              headers: {
                  'X-CSRF-TOKEN': token
              }
          };
          $http(req)
              .then(function (response) {
                  if(response.data.name != '')
                  {
                      $state.go('app.dashboard');
                  }
                  else
                  {
                      $state.go('core.login');
                  }
  
  
              });
  
      };
  // and fire it after definition
      init();
      $scope.login = function($Login_form) {
          var req = {
              method: 'POST',
              url: baseUrl + 'login/check',
              headers: {
                  'X-CSRF-TOKEN': token
              },
              data: $scope.user
          };
  
          $http(req)
              .then(function (response) {
                  if(response.data.status == 'success')
                  {
                      $state.go('app.dashboard');
                      toastr.success('', 'Logged in successfully');
                  }else
                  {
                      toastr.warning('', response.data.message);
                  }
              });
  
      };
  </code></pre>
  
  <p>Now the <code>init()</code> does is that it goes to my Laravel controller and checks is user is logged in if not then returns to login page else other wise to dashboard.</p>