且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何避免显示,如果已经与angularjs登录登录页面?

更新时间:2022-10-23 09:28:28

我觉得普遍的做法是听 $ routeChangeStart 事件。

  $范围。在$('$ routeChangeStart'功能(适用范围,接下来,电流){
   // ...
});

在这里你可以问,如果下一个路径是登录,然后你可以检查AUTH服务,如果用户已经登录。如果他是,使用重定向像往常一样给他直接重定向到登录

alternativally您可以使用解析回调在你的路由对象

 时,('/登录',{控制器:'LoginCtrl',templateUrl:'',解决:{
    载:函数(...){...}
})

目前已有类似的东西在这里的SO

AngularJS - 需要$ routeChangeStart的某种组合和$ locationChangeStart

Fire回调时路由在AngularJS

改变(动画,装载机等)

I am building an application with AngularJS. I have authentication working and when the user is not authorized, he gets redirected to #/login which shows a login form.

However, when the user is authenticated and he manually goes to #/login, the login page is shown again, although the user is still logged in. Is there any way to direct him to the home page in that case?

This is how I do the redirections:

$routeProvider
       .when( '/ds', {
            templateUrl: 'partials/datasheets-list.html',
            controller: 'DatasheetsListCtrl'
        } )
        .when( '/ds/:datasheetId', {
            templateUrl: 'partials/datasheet-detail.html',
            controller: 'DatasheetDetailCtrl'
         } )                             
         .when( '/login', {
            templateUrl: 'partials/login.html',
            controller: 'LoginController'
          } )
         .otherwise( {
            redirectTo: '/ds'
          } );

This is the code for the LoginController:

var datasheetsControllers = angular.module( 'datasheetsControllers', ['ngCookies', 'datasheetsServices'] );

datasheetsControllers.controller( 'LoginController', ['$scope', '$rootScope', '$location', '$http', '$cookieStore', 'LoginService', function ( $scope, $rootScope, $location, $http, $cookieStore, LoginService )
{

    $scope.login = function ()
    {
        LoginService.authenticate( $.param( {username: $scope.username, password: $scope.password} ), function ( user )
        {
            $rootScope.user = user;
            // Authenticate AngularJS Ajax calls
            $http.defaults.headers.common[ xAuthTokenHeaderName ] = user.token;

            // Authenticate jQuery Ajax calls
            var headers = {};
            headers[xAuthTokenHeaderName] = user.token;
            $.ajaxSetup({
                            headers: headers
                        });
            $cookieStore.put( 'user', user );
            $location.path( "/" );
        } );
    };
}] );

I think the common practice is to listen to the $routeChangeStart event.

$scope.$on('$routeChangeStart', function(scope, next, current){
   //...
});

here you can ask if the next path is login and then you can check for the auth service if the user is already logged in. if he is, use redirect as usual to redirect him directly to the login

alternativally you can use the resolve callback in your route object

when('/login', { controller: 'LoginCtrl', templateUrl: '', resolve : {
    load : function ( ... ) { ... }
})

There is already similar stuff here on SO

AngularJS - Need some combination of $routeChangeStart and $locationChangeStart

Fire a callback when route is changed (animations, loaders etc) in AngularJS