ng-token-auth icon indicating copy to clipboard operation
ng-token-auth copied to clipboard

Redirect if authenticated

Open williamweckl opened this issue 9 years ago • 8 comments

Here are my routes:

this.app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider
    .when('/', {
        templateUrl: 'views/site/layouts/layout.html',
        controller: 'SiteIndexCtrl'
    })
    .when('/login', {
        templateUrl: 'views/site/layouts/layout.html',
        controller: 'SignInCtrl'
    })
;}]);

How can I do to redirect if already logged in when user enter in /login?

williamweckl avatar Mar 20 '15 00:03 williamweckl

Hello! On my project I did it with angular-permission. Note that angular-permissions works only with ui-router.

It's how I define anonymous role

angular.module('app')
    .run(function($rootScope, Permission) {
        Permission.defineRole('anonymous', function (stateParams) {
            // If the returned value is *truthy* then the user has the role, otherwise they don't
            if (!$rootScope.user.signedIn) {
                return true; // Is anonymous
            }

            return false;
        });
    }

It's config of state

angular.module('app')
    .config(function ($stateProvider) {
        $stateProvider
            .state('login', {
                url: '/login',
                templateUrl: 'app/auth/login.tmpl.html',
                controller: 'LoginController as login',
                data: {
                    permissions: {
                        only: ['anonymous'],
                        redirectTo: 'homepage'
                    }
                }
            });
    }

askobara avatar Mar 20 '15 17:03 askobara

@askobara Hi! Just read your comment. How did you setup permission with ng-token-auth? My $rootScope.user is undefined and I'm nto sure where to define the persmissions. This is a part of my code inside $stateProvider:

    .state('app', {
      abstract: true,
      templateUrl: "<%= asset_path('common/content.html') %>",
      controller: "AppCtrl as app",
      resolve: {
        auth: function($auth, $state) {
          return $auth.validateUser().catch(function(){
            // redirect unauthorized users to the login page
            $state.go('auth');
          });
        }
      }
    })

I'm trying with the following a bit further down:

function permissions(Permission, $rootScope, $auth) {
  // Define permissions
}

angular
  .module('myapp')
  .config(config)
  .run(function($rootScope, $state) {
    $rootScope.$state = $state;
  })
  .run(permissions)

stefanahman avatar Apr 22 '15 19:04 stefanahman

Hello, @stefanahman You have to define Permission.defineRole(...) into .run() phase of your application and make sure that run method of your module is called after ng-token's. On this phase ng-token-auth should already finished his work and $rootScope contains user data.

I updated my previous answer for more clearly understanding.

P.S. Sorry for late answer. I missed this notification...

askobara avatar May 04 '15 14:05 askobara

Is there a way to do it without using angular-permission ? thanks

m2omou avatar Feb 02 '16 01:02 m2omou

@m2omou with ui-router, work for me

@angularTodo.run([
  '$rootScope'
  '$state'
  '$auth'
  ($rootScope, $state, $auth) ->
    $rootScope.$on '$stateChangeStart', (event, toState, toParams, fromState) ->
      $auth.validateUser().then (response) ->
        if toState.name == 'sign_in' || toState.name == 'sign_up'
          if !angular.equals({}, response)
            $state.go('projects')
            event.preventDefault()
])

eoris avatar Apr 19 '16 11:04 eoris

This is @eoris code without the cafeine

this.angularTodo.run([
  '$rootScope', '$state', '$auth', function($rootScope, $state, $auth) {
    return $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState) {
      return $auth.validateUser().then(function(response) {
        if (toState.name === 'sign_in' || toState.name === 'sign_up') {
          if (!angular.equals({}, response)) {
            $state.go('projects');
            return event.preventDefault();
          }
        }
      });
    });
  }
]);

angelxmoreno avatar May 03 '16 04:05 angelxmoreno

This is @eoris code without the cafeine

this.angularTodo.run([
  '$rootScope', '$state', '$auth', function($rootScope, $state, $auth) {
    return $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState) {
      return $auth.validateUser().then(function(response) {
        if (toState.name === 'sign_in' || toState.name === 'sign_up') {
          if (!angular.equals({}, response)) {
            $state.go('projects');
            return event.preventDefault();
          }
        }
      });
    });
  }
]);

Is $stateChangeStart a predefined service here? Or is it defined somewhere?

pierrettemugisha avatar Jul 08 '16 19:07 pierrettemugisha

@pierrettemugisha https://github.com/angular-ui/ui-router/wiki#state-change-events

eoris avatar Jul 09 '16 07:07 eoris