将经过身份验证的用户重定向到无效 url 命中的主页

Redirect Authenticated user to home page for invalid url hit

我有一个简单的 jhipster 应用程序。我希望经过身份验证的用户重定向到无效 url 的主页。之前我更改了 home.state.js 并添加了 login.state.js 如下所述:

home.state.js

(function() {
    'use strict';

    angular
        .module('anvilIqApp')
        .config(stateConfig);

    stateConfig.$inject = ['$stateProvider'];

    function stateConfig($stateProvider) {
        $stateProvider.state('home', {
            parent: 'app',
            url: '/home',
            data: {
                authorities: ['ROLE_USER']
            },
            views: {
                'content@': {
                    templateUrl: 'app/home/home.html',
                    controller: 'HomeController',
                    controllerAs: 'vm'
                }
            },
            resolve: {
                mainTranslatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate,$translatePartialLoader) {
                    $translatePartialLoader.addPart('home');
                    return $translate.refresh();
                }]
            }
        });
    }
})();

login.state.js

    (function() {
    'use strict';

    angular
        .module('anvilIqApp')
        .config(stateConfig);

    stateConfig.$inject = ['$stateProvider'];

    function stateConfig($stateProvider) {
        $stateProvider.state('login', {
            parent: 'app',
            url: '/',
            data: {
                authorities: []
            },
            views: {
                'content@': {
                    templateUrl: 'app/components/login/login.html',
                    controller: 'LoginController',
                    controllerAs: 'vm'
                }
            },
            resolve: {
                mainTranslatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate,$translatePartialLoader) {
                    $translatePartialLoader.addPart('login');
                    return $translate.refresh();
                }]
            }
        });
    }
})();

如您所见,根现在是登录页面。当经过身份验证的用户点击无效 url 时,将照常显示登录页面。对于无效的 url 命中,我想将用户重定向到 /home

里面 auth.service.js 我有这个片段:

function authThen () {
    var isAuthenticated = Principal.isAuthenticated();

    // an authenticated user can't access to login and register pages
    if (isAuthenticated && $rootScope.toState.parent === 'account' && ($rootScope.toState.name === 'login' || $rootScope.toState.name === 'register')) {
        $state.go('home');
    }

    // recover and clear previousState after external login redirect (e.g. oauth2)
    if (isAuthenticated && !$rootScope.fromState.name && getPreviousState()) {
        var previousState = getPreviousState();
        resetPreviousState();
        $state.go(previousState.name, previousState.params);
    }

    if ($rootScope.toState.data.authorities && $rootScope.toState.data.authorities.length > 0 && !Principal.hasAnyAuthority($rootScope.toState.data.authorities)) {
        if (isAuthenticated) {
            // user is signed in but not authorized for desired state
            $state.go('accessdenied');
        }
        else {
            // user is not authenticated. stow the state they wanted before you
            // send them to the login service, so you can return them when you're done
            storePreviousState($rootScope.toState.name, $rootScope.toStateParams);

            // now, send them to the signin state so they can log in
            $state.go('accessdenied').then(function() {
                $state.go('login');
            });
        }
    }
} 

您可以使用 $urlRouterProvider 中提供的其他选项。注入 $urlRouterProvider 并在其中传递默认状态。

$urlRouterProvider.otherwise('/home');

因此,对于 URL 以外的定义的将重定向到传入的 URL。

更多详情:http://angular-ui.github.io/ui-router/site/#/api/ui.router.router.$urlRouterProvider