2016-09-10 10 views
0

私はSPAにセキュリティフレームワークauth0を統合しています。私はそのチュートリアルに続きました: https://auth0.com/docs/quickstart/spa/angularjsAuth0 Lock10とui-router

すべてがうまく見え、設定されています。しかし、私はどのようにui-router上の状態を確保するために見つけることができませんでした。 バージョン9では、ui-routerで{requiresLogin:true}というデータを使用していました。私はlock10でそれを行う方法を見つけられなかったし、チュートリアルではそれについて何も持っていない。

githubのサンプルでは、​​まだ古いバージョンのlock9を使用しています。誰かがどのようにして州に強制的にログインを要求できるのか知っていますか?

+0

あなたがhttp://stackoverflow.com/questions/35924577/restrict-unauthorised-access-in-component-angular-2を使用してみましたか? – ShrekOverflow

+0

申し訳ありません、これは角度1です –

答えて

1

この機能は削除されているようです。私は与えられた状態でログインするユーザーを必要とするかどうかを検出するために、非Auth0固有の方法に行き、私のangular.run(..)に次を追加しました:

$rootScope 
    .$on('$stateChangeStart', function (e, toState, toParams, fromState, fromParams) { 
      if (toState.data && toState.data.requiresLogin === true) { 
       if (!authService.isAuthenticated()) { 
        e.preventDefault(); 
        $state.go('root.login', { toState: toState, toParams: toParams }); 
       } 
      } 
     }); 

私のAuthServiceは、彼らがAuth0に規定するものと同じですあなたにリンクされた例を提供しましたが、私はisAuthenticated()メソッドも追加しました。もちろん$rootScopeangular.run(..)に注入して確認することもできます。

うまくいけば、私たちの一人がLock10内でそれを行う方法を学ぶことができますが、それまではこれは空を埋めるでしょう。

0

ui-routerdeprecated$stateChange*以来持っているので、これを試してみてください。

/*jslint latedef:false*/ 
(function() { 

    'use strict'; 

    angular 
    .module('myApp') 
    .run(run); 

    run.$inject = ['authService', '$transitions']; 

    function run(authService, $transitions) { 

    $transitions.onStart({ 
     to: function(state) { 
     return state.data && state.data.requiresLogin; 
     } 
    }, function(trans) { 
     if (!authService.isAuthenticated()) { 
     authService.login(); 
     // or if you have a login route: 
     // return trans.router.stateService.target('login'); 
     } 
    }); 
    } 

})();