user2024080
user2024080

Reputation: 5101

How to handle different scenario in `angular route` in correct standard?

I am developing a nodejs application using angular. here I just confused with following scenarios. i require the best practice to know to handle all this:

In route

  1. How to show some pages, without authentication?
  2. How to show some other pages, only after authentication success?
  3. How to re-direct wrong urls ( which is not in app ) - without authentication?
  4. How to re-direct wrong urls to home page or back page after authentication?

All this scenario is to handled in fronend or backend? else what this the correct way?

here is samples:

Never Required to test the authentication:

.when('/login', {
    templateUrl : 'app/views/pages/login.html',
    controller : 'mainController',
    controllerAs: 'login'
})

.when('/login', {
    templateUrl : 'app/views/pages/createUser.html',
    controller : 'userController',
    controllerAs: 'user'
})

Always required Authentication :

.when('/login', {
    templateUrl : 'app/views/pages/showProduct.html',
    controller : 'productController',
    controllerAs: 'product'
})
.when('/login', {
    templateUrl : 'app/views/pages/showStatics.html',
    controller : 'staticController',
    controllerAs: 'static'
})

before authentication with wrong URL:

.when('/login', {
    templateUrl : 'app/views/pages/testingWrong'
})

After authentication with wrong URL :

.when('/static', {
    templateUrl : 'app/views/pages/showStatics.html',
    controller : 'staticController',
    controllerAs: 'static'
})
.when('/static/wrong', {
    templateUrl : 'app/views/pages/showStatics.html',
})

If there is a tutorial, please share me. or if there is a solution please post with details to understand.

Thanks in Advance.

Upvotes: 0

Views: 44

Answers (1)

Ahemad Tutake
Ahemad Tutake

Reputation: 46

You need add resolve function param, just like below

when('products', {
   templateUrl : 'app/views/pages/showProduct.html',
   controller : 'productController',
   controllerAs: 'product',
   resolve: function($q, $location, Auth) {
      var deferred = $q.defer();
      deferred.resolve();
      if (!Auth.isLoggedIn()) {
         $location.path('/login');
      }
      return deferred.promise;
   }
});

Auth will be some service which keeps user's logged status. Hope this will help you.

Upvotes: 2

Related Questions