Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular default page routing

AngularJS(1) is used in my UI. I need change behavior for default page. If user is not authorized he should navigate to login page other case -> special page.

Configuring anonymous module

anonimModule.config(function ($routeProvider, anonimTemplateManager, anonimUrlManager) {
  $routeProvider
      .when('/', {
          templateUrl: anonimTemplateManager.getLoginPath(),
          controller: 'loginCtrl'
      })

I can set only one time this behavior (only for login for example) but the special page is not accessible if user authorized. I tried to get access to $rootScope from when (to solve my issue) - unsuccessfully.

Is it possible to implement dynamic behavior for routing? Could I have example or idea to implement it please?

like image 707
Sergii Avatar asked Jul 01 '26 22:07

Sergii


2 Answers

Try to use otherwise.

Just chain it, like so:

.when('/foo', {
   templateUrl: 'foo.html',
   controller: fooController
)
.when('/bar', {
   templateUrl: 'bar.html',
   controller: barController
)
.otherwise(
    redirect: '/foo'
)
like image 108
Bas Pauw Avatar answered Jul 04 '26 13:07

Bas Pauw


You may use state routing and then you may use statechangestart event for example:-

.state('user.dashboard', {
        templateUrl: 'pages/my/dashboard/deshboard.html',
        controller: 'myDashboardController'
        url: '/dashboard',
         access: {
            loginRequired: true
        }

and then in app.js you may use $stateChangeStart and some auth service that will check if user is authrize to access that state or not

 $rootScope.$on('$stateChangeStart',
            function (event, toState) {
                var authorised;
                if (toState.access !== undefined) {
                    authorised = authorizationServie.authorize(toState.name);
                   if (authorised ===  false) {
                        $state.go("loginState");
                        event.preventDefault();
                    }
                }
            });
like image 33
jitender Avatar answered Jul 04 '26 11:07

jitender