2016-04-14 2 views
0

こんにちは、私はAngularJSの新機能です。最近、AngularJSプロジェクトに導入されました。今バックエンドで私はlaravelを使用しています。彼らがAngularの方法であるかどうかを知りたいのですが、私が作成できるのは、ミドルウェアがlaravelで行っているのと同じ機能を使います。私は基本的に欲しいAngularJSのミドルウェアタイプの機能は?

は、私は、ユーザーが、その後にログインしている場合にのみ、彼は他のページへの応用に前進できる必要があるということです。..

私が何かを試してみましたが、私はそれが正しいと感じることはありません。

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); 
       } 
      }); 

    }; 

init()はありません、それは私のlaravelコントローラとチェックに行くことであるユーザーでログインしていない場合は、その後のダッシュボードに賢明な他の他のログインページに戻りますされています。

答えて

1

私はそれが一般的な問題だと私はそれも解決しなければならないと思います。

基本的には、ui-routerを使用して認証が必要な状態について、ユーザーがログに記録されているかどうかを証明する認証機能を呼び出すことができます(アクセスする役割があるかどうかも確認します) 。

ユーザー状態を格納するためのサービスの使用をお勧めします。

myApp.config(function($stateProvider, $urlRouterProvider) { 
    // 
    // For any unmatched url, redirect to /home 
    $urlRouterProvider.otherwise("/home"); 
    // 
    // Now set up the states 
    $stateProvider 
    .state('home', { 
     url: "/home", 
     templateUrl: "partials/home.html", 
     controller: 'homeController', 
     controllerAs: 'homeCtrl' 
    }) 
    .state('contact', { 
     url: "/contact", 
     templateUrl: "partials/contact.html", 
     controller: 'contactController', 
     controllerAs: 'contactCtrl' 
    }) 

    .state('profile', { 
     url: '/profile', 
     templateUrl: 'partials/profile.html', 
     controller: 'profileCtrl', 
     resolve: { 
      authenticated: authentic 
     } 
    }); 


    function authentic($q, $location, $auth) { 
     var deferred = $q.defer(); 

     if (!$auth.isAuthenticated()) { 
      $location.path('/login'); 
     } else { 
      deferred.resolve(); 
     } 
     return deferred.promise; 
    }; 
}); 

私はそれが役に立ちそうです。

関連する問題