AngularJs - best way to limit access to 'logged in' users

↘锁芯ラ 提交于 2019-11-29 20:20:12

You can intercept route changes as you suggested and act accordingly, using the following example as a basis:

    $rootScope.$on('$routeChangeStart', function (event, next) {
        var userAuthenticated = ...; /* Check if the user is logged in */

        if (!userAuthenticated && !next.isLogin) {
            /* You can save the user's location to take him back to the same page after he has logged-in */
            $rootScope.savedLocation = $location.url();

            $location.path('/User/LoginUser');
        }
    });

Also, add isLogin: true to the route definition of your login page, like this:

$routeProvider
    // LOGIN
    .when('/User/LoginUser', {templateUrl: 'views/user/login.html',controller: 'loginCtrl', isLogin: true})

Good luck with your project!

My opinion:

<?php if (!$_SESSION['user_id'] { forward(/user/access); }) ?>

and here comes your angular app...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!