简体   繁体   中英

Open link into tab redirect to the login page instead of given url

I am working with angularjs. When I right click on link and open it to new tab then it redirect to login page instead of given url.

<ul class="nav nav-sidebar">
    <li><a ui-sref="dashboard"><i class="icon-home"></i><span>Dashboard</span></a></li>
    <li class="nav-parent">
        <a><i class="icon-external-link"></i><span>Masters</span> <span class="fa arrow"></span></a>
        <ul class="children collapse">
            <li><a ui-sref="dashboard.masters_userroles"> User Roles</a></li>
            <li><a ui-sref="dashboard.masters_countries"> Countries</a></li>
            <li><a ui-sref="dashboard.masters_states"> States</a></li>
            <li><a ui-sref="dashboard.masters_cities"> Cities</a></li>
            <li><a ui-sref="dashboard.masters_departments"> Departments</a></li>
        </ul>
    </li>
    <li><a ui-sref="dashboard.ticket"><i class="icon-ticket"></i><span>Ticket</span></a></li>
    <li class="nav-parent">
        <a><i class="icon-tasks"></i><span>Manage</span> <span class="fa arrow"></span></a>
        <ul class="children collapse">
            <li><a ui-sref="dashboard.people">People</a></li>
        </ul>
    </li>
</ul>

Here, I have set state that looks like below :

app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise('/');

$stateProvider

    // Auth routes =================================
    .state('login', {
        url: '/login',
        templateUrl: 'partials/user-login.html',
        data: {
            bodyClasses: 'account separate-inputs'
        }
    })

    .state('signup', {
        url: '/signup',
        templateUrl: 'partials/user-signup.html',
        data: {
            bodyClasses: 'account separate-inputs boxed'
        }
    })

    // Dashboard and nested partials ========================================
    .state('dashboard', {
        url: '/',   
        templateUrl: 'partials/dashboard.html',
        data: {
            bodyClasses: ''
        },
        resolve: {
            auth: ["$q", "authenticationSvc", "$state", function($q, authenticationSvc, $state) {
                var userInfo = authenticationSvc.getUserInfo();
                if (userInfo) {
                    return $q.when(userInfo);
                } else {
                    return $q.reject({ authenticated: false });
                }
            }]
        }
    })

    .state('dashboard.profile', {
        url: 'profile',
        views: {
            'page@dashboard' : {
                templateUrl: 'partials/profile.html'
            }
        }
    })

    .state('dashboard.masters_userroles', {
        url: 'masters/userroles',
        views: {
            'page@dashboard' : {
                templateUrl: 'partials/masters/userroles.html'
            }
        }
    })

    .state('dashboard.masters_countries', {
        url: 'masters/countries',
        views: {
            'page@dashboard' : {
                templateUrl: 'partials/masters/countries.html'
            }
        }
    })

    .state('dashboard.masters_states', {
        url: 'masters/states',
        views: {
            'page@dashboard' : {
                templateUrl: 'partials/masters/states.html'
            }
        }
    })

    .state('dashboard.masters_cities', {
        url: 'masters/cities',
        views: {
            'page@dashboard' : {
                templateUrl: 'partials/masters/cities.html'
            }
        }
    })

    .state('dashboard.masters_departments', {
        url: 'masters/departments',
        views: {
            'page@dashboard' : {
                templateUrl: 'partials/masters/departments.html'
            }
        }
    })

    .state('dashboard.ticket', {
        url: 'ticket/viewTicket',
        views: {
            'page@dashboard' : {
                templateUrl: 'partials/ticket/view_ticket.html'
            }
        }
    })

    .state('dashboard.addTicket', {
        url: 'ticket/addTicket',
        views: {
            'page@dashboard' : {
                templateUrl: 'partials/ticket/add_ticket.html'
            }
        }
    })

    .state('dashboard.manageTicket', {
        url: 'ticket/manageTicket',
        views: {
            'page@dashboard' : {
                templateUrl: 'partials/admin/ticket/manage_ticket.html'
            }
        }
    })

    .state('dashboard.people', {
        url: 'admin/people',
        views: {
            'page@dashboard' : {
                templateUrl: 'partials/admin/people/people.html'
            }
        }
    });

}]);

//there is run method in this file.

app.run( function ( $rootScope, $state, $location, $window) {
var vm = $rootScope;
$rootScope.$on('$stateChangeSuccess', function() {
    if($window.sessionStorage["userInfo"])
    {
        $rootScope.currentUser = JSON.parse($window.sessionStorage["userInfo"])["currentUser"]["data"];
    }
    var currentState = $state.current.name.split(".");
    vm.datapage = currentState[currentState.length - 1];
    if (angular.isDefined($state.current.data)) {
        vm.bodyClasses = $state.current.data.bodyClasses;
    }
    var allowedStates = ["signup"];
    if(!$window.sessionStorage.getItem('userInfo') && !(allowedStates.includes($state.current.name)))
        $state.go("login");
});

$rootScope.$on('$stateChangeError', function(evt, toState, toParams, fromState, fromParams, error) {
    if (error.authenticated == false) {
        $state.go("login");
    }
});
});

When I open these link's to another tab it redirect to login page. So, how can I solve this error?

Note : Here, every request has header in which token is set.If token is not set than it redirect to the login page. This is the logic for every request.

From ui-router docs :

ui-sref

A directive that binds a link (<a> tag) to a state. If the state has an associated URL, the directive will automatically generate & update the href attribute via the $state.href() method . Clicking the link will trigger a state transition with optional parameters. Also middle-clicking, right-clicking, and ctrl-clicking on the link will be handled natively by the browser.

The directive updates the href attribute, so you can't set it in the same tag.

Change your from:

<a href="..." ui-sref="...">

To

<a ui-sref="...">
<li><a ui-sref="dashboard.masters_userroles"> User Roles</a></li>

here do not use href if you are using ui-sref . Remove href if you are using ui-sref on a single anchor element.

and you pass values using ui-sref

ui-sref="dashboard.masters_userroles({bodyClasses: 'tokenvalue'})"

You did not pass any token value using ui-sref

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM