在角度js中验证后发送回请求的页面;需要更好的方法

时间:2016-07-06 09:07:04

标签: angularjs authentication

我有3页A,B和C. A要求登录;

  • 用户点击A
  • 检查身份验证(用户是否登录)
  • 如果用户未登录而不是登录页面
  • 成功登录后,发回A

现在我到目前为止做了什么;这是我的 working plunker

  1. params 添加到登录状态,并在 .config 块中设置为null

    $stateProvider
      .state("login", {
            url: '/login',
            controller: 'LoginController',
            data : { pageTitle : 'Home', requiresAuth: true }
            params : {'returnTo' : null }
    });
    
  2. 当用户点击经过身份验证的页面时,我们会将用户发送到登录页面,并在$state.target

    中添加其他参数
    $transitions.onBefore(requiresAuthCriteria, redirectToLogin, {priority: 10} );
    
    var requiresAuthCriteria = {
    to: function (state) {
        return state.data && state.data.requiresAuth;
    }
    };
    var redirectToLogin = function($transition$, $injector) {
    var AuthService = $injector.get('AuthService');
    if (!AuthService.isAuthenticated()) {
        return $state.target('login', { returnTo : $transition$.to().name });
    }
    

    };

  3. 登录控制器;成功登录后,我们将参数绑定到rootScope.broadcast事件

     login
         .controller('LoginController', function($rootScope, AUTH_EVENTS, AuthService, $stateParams){
         var self = this;
        self.login = function(credentials){
        AuthService.login(credentials).then(function(user){
            $rootScope.returnTo = $stateParams.returnTo;
            $rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
        }, function (){
            $rootScope.$broadcast(AUTH_EVENTS.loginFailed);
        });
    };
    
  4. .run阻止我们捕获此广播事件,并通过$state.go

    将用户发送到请求的页面
          $rootScope.$on(AUTH_EVENTS.loginSuccess, function(event) {
             $timeout(function() { $state.go($rootScope.returnTo); });
          });
    
  5. 但我认为,这不是一种有效的方法,因为我还需要检查身份验证;我尝试使用 resolve

    .state
    resolve : { returnTo : returnTo }
    
    function returnTo ($transition$) {
        var redirectedFrom = $transition$.previous();
        // The user was redirected to the login state (via the requiresAuth hook)
        if (redirectedFrom !== null) {
            // Follow the current transition's redirect chain all the way back to the original attempted transition
            while (redirectedFrom.previous()) {
              redirectedFrom = redirectedFrom.previous();
            }
            return { state: redirectedFrom.to(), params: redirectedFrom.params("to") };
        }
        var fromState = $transition$.from();
        var fromParams = $transition$.params("from");
        console.log(fromState);
        if (fromState.name !== '') {
            return {state: fromState, params: fromParams};
        }
        // If the fromState's name is empty, then this was the initial transition. Just return them to the home state
        return { state: 'base' };
    }
    

    但不明白为什么这不起作用;请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

经过各种文章的大量浏览后得到了解决方案

现在替代方法是使用 pos = pos[satIDs] 属性;删除resolve属性,这是所有代码

  1. 在登录状态下,添加params属性并添加resolve属性,以便用户在登录后无法访问 / login

    onEnter
  2. 定义 .state("login", { url: '/login', templateUrl: './user/login.html', controller: 'LoginController', controllerAs : 'vm', resolve: { returnTo: returnTo }, onEnter: function($state, AuthService){ if(AuthService.isAuthenticated()) { return $state.target('base', undefined, { location: false }); } } }) 函数copied from here

    returnTo
  3. 真正的谜团是这个 function returnTo ($transition$) { var redirectedFrom = $transition$.previous(); // The user was redirected to the login state (via the requiresAuth hook) if (redirectedFrom !== null) { // Follow the current transition's redirect chain all the way back to the original attempted transition while (redirectedFrom.previous()) { redirectedFrom = redirectedFrom.previous(); } // return to the original attempted "to state" return { state: redirectedFrom.to(), params: redirectedFrom.params("to") }; } // The user was not redirected to the login state; they directly activated the login state somehow. var fromState = $transition$.from(); var fromParams = $transition$.params("from"); if (fromState.name !== '') { return {state: fromState, params: fromParams}; } // If the fromState's name is empty, then this was the initial transition. Just return them to the home state return { state: 'base' }; } 如何在函数内部可用!!!,有人可以解释而不是感恩

    1. 现在在 .loginController 中注入$transitions$$state(函数名称),成功登录后,使用{{1}更改状态从returnTo函数

      返回的对象和state
      params
    2. 希望它可以帮助某人