Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yeoman Angular Fullstack - how to secure routes?

I have created a new route 'Rooms' with the generator and have modelled my $stateProvider on the admin route

.state('admin', {
    url: '/admin',
    templateUrl: 'app/admin/admin.html',
    controller: 'AdminController',
    controllerAs: 'admin',
    authenticate: 'admin'
  });

Vs

$stateProvider
  .state('rooms', {
    url: '/rooms',
    templateUrl: 'app/rooms/rooms.html',
    controller: 'RoomsCtrl',
    controllerAs: 'rooms',
    authenticate: 'admin'
  });

But my route still appears without authentication!

I guess I am missing a few things to make it secure, though I am unable to understand what!

Can anyone help?

Thanks

like image 817
stevejvv Avatar asked Mar 09 '26 00:03

stevejvv


1 Answers

your controller should be like:

     angular.module('auth8App').controller('RoomsCtrl', function ($scope,Auth,$location) {

               //check if the user is logged-in
                Auth.isLoggedInAsync(function(loggedIn) {
                        if (!loggedIn) {    
                       //if the user is not logged  Redirect to login
                          event.preventDefault();
                          $location.path('/login');
                        }
                      });
                    $scope.message = 'Hello';
                });
like image 189
aitnasser Avatar answered Mar 10 '26 14:03

aitnasser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!