加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angularjs ui路由器 如何重定向到登录页面

发布时间:2020-12-17 09:02:01 所属栏目:安全 来源:网络整理
导读:我有4个状态:仪表板,dahboard.main,dashboard.minor,登录。 仪表板是抽象的,它是.minor和.main状态的父状态。 下面是我的代码: .state('dashboard',{ url: "/dashboard",abstract: true,templateUrl: "views/dashboard.html",resolve: { auth: functio
我有4个状态:仪表板,dahboard.main,dashboard.minor,登录。
仪表板是抽象的,它是.minor和.main状态的父状态。
下面是我的代码:
.state('dashboard',{
        url: "/dashboard",abstract: true,templateUrl: "views/dashboard.html",resolve: {
            auth: function ($q,authenticationSvc) {
                var userInfo = authenticationSvc.getUserInfo();
                if (userInfo) {
                    return $q.when(userInfo);
                } else {
                    return $q.reject({ authenticated: false });
                }
            }
        },controller: "DashboardCtrl",data: { pageTitle: 'Example view' }
    })
    .state('dashboard.main',{
        url: "",templateUrl: "views/main.html",data: { pageTitle: 'Main view' }
    })

正如你在仪表板状态我看到解决选项。通过这个,我想重定向用户到登录页面,如果他没有授权。为此,我使用特殊的authenticationSvc服务:

.factory("authenticationSvc",["$http","$q","$window",function ($http,$q,$window) {
    var userInfo;

    function login(email,password) {
        var deferred = $q.defer();

        $http.post("/api/login",{ email: email,password: password })
            .then(function (result) {
                if(result.data.error == 0) {
                    userInfo = {
                        accessToken: result.data.accessToken
                    };
                    $window.sessionStorage["userInfo"] = JSON.stringify(userInfo);
                    deferred.resolve(userInfo);
                }
                else {
                    deferred.reject(error);
                }
            },function (error) {
                deferred.reject(error);
            });

        return deferred.promise;
    }
    function getUserInfo() {
        return userInfo;
    }
    return {
        login: login,logout: logout,getUserInfo: getUserInfo
    };
}]);

我检查auth值在config:

.run(function($rootScope,$location,$state) {
    $rootScope.$state = $state;
    $rootScope.$on("routeChangeSuccess",function(userInfo) {
        consol.log(userInfo);
    });
    $rootScope.$on("routeChangeError",function(event,current,previous,eventObj) {
        if(eventObj.authenticated === false) {
            $state.go('login');
        }
    });
});

但不幸的是,当我去我的网站根或仪表板状态,我得到一个空页面。这个代码有什么问题?谢谢!

关键是,如果不需要,不重定向===如果已经重定向到预期的状态。有 a working plunker有类似的解决方案
.run(function($rootScope,$state,authenticationSvc) {


    $rootScope.$on( '$stateChangeStart',function(e,toState,toParams,fromState,fromParams) {

        var isLogin = toState.name === "login";
        if(isLogin){
           return; // no need to redirect 
        }

        // now,redirect only not authenticated

        var userInfo = authenticationSvc.getUserInfo();

        if(userInfo.authenticated === false) {
            e.preventDefault(); // stop current execution
            $state.go('login'); // go to login
        }
    });
});

检查这些类似的解释:

> Angular ui router – Redirection doesn’t work at all
> How can I fix ‘Maximum call stack size exceeded’ AngularJS

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读