Redirecting to a corner redirection for authorization

I created an entry system with corner fire and firebase.

I have a function that is called when the user logs in and several times. Checks if authData exists and registers the user as registered if one exists.

fbRef.onAuth(authDataCallback);
function authDataCallback(authData) {
    if (authData) {
        $scope.loggedIn = true;
    } else {
        $scope.loggedIn = false;
    }
}

I also have routes installed

.config(function($routeProvider) {
    $routeProvider
    .when('/', {
        controller: '',
        templateUrl: '/company/pages/account/pages/dashboard.php',
    })
    .when('/dashboard', {
        controller: '',
        templateUrl: '/company/pages/account/pages/dashboard.php',
    })
    .when('/login', {
        controller: 'Authorization',
        templateUrl: '/company/pages/account/pages/login.php',
    })
    .when('/register', {
        controller: 'Authorization',
        templateUrl: '/company/pages/account/pages/register.php',
    })
})

I want every page, except for the login and registration pages, to be limited only to enter the system. I want the user to be redirected to the login page if it $scope.loggedInreturns false.

I tried many solutions on the Internet, but no one worked for me. Most of them led to exceeding the limit.

How to restrict access in my situation?

+4
source share
3

, "loggedIn"

    app.controller('MyCtrl', ['$scope', '$location', function($scope, $location){

        fbRef.onAuth(authDataCallback);

        $scope.loggedIn = null;

        function authDataCallback(authData) {
            $scope.loggedIn = authData ? true : false
        }

        $scope.$watch("loggedIn", function(val){

            val === false && $location.path("/login");

        });

    });
0

, , .

app.controller('MyCtrl', ['$scope', '$location', function($scope, $location) {

  fbRef.onAuth(authDataCallback);

  function authDataCallback(authData) {
      authData ? $location.path("/dashboard") : $location.path("/login");
  }

}]);
0

Plan:

Another possible solution is to set a variable called loginRequired, from, $routeProvider and from $routeChangeStartto check the variable for $rootScope.loggedIn.

Unfortunately, as you can see, you need to move $scope.loggedInto $rootScope.loggedIn.

One possible implementation:

In the router:

.config(function($routeProvider) {
    $routeProvider
        .when('/', {
            controller: '',
            templateUrl: '/company/pages/account/pages/dashboard.php',
            loginRequired: true
        })
        .when('/dashboard', {
            controller: '',
            templateUrl: '/company/pages/account/pages/dashboard.php',
            loginRequired: true
        })
        .when('/login', {
            controller: 'Authorization',
            templateUrl: '/company/pages/account/pages/login.php',
            loginRequired: false
        })
        .when('/register', {
            controller: 'Authorization',
            templateUrl: '/company/pages/account/pages/register.php',
            loginRequired: true
        })
    })

In app.run:

app.run(
    ['$rootScope', '$location', function($rootScope, $location) {

        $rootScope.$on('$routeChangeStart', function() {
            if ($rootScope.loginRequired && !$rootScope.loggedIn) {
                $location.path("/login");
            }
        });

    }]);
0
source

All Articles