ng-token-auth
ng-token-auth copied to clipboard
Redirect if authenticated
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?
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 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)
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...
Is there a way to do it without using angular-permission
? thanks
@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()
])
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();
}
}
});
});
}
]);
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 https://github.com/angular-ui/ui-router/wiki#state-change-events