From f5c0d673550f29e97a299d564777c7df03e20098 Mon Sep 17 00:00:00 2001 From: mtvs Date: Mon, 20 Jun 2016 20:04:12 +0430 Subject: [PATCH 1/2] fix(client:auth.decorator): redirect the user to the login page if is not logged in --- .../app/client/components/auth(auth)/router.decorator.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/app/client/components/auth(auth)/router.decorator.js b/templates/app/client/components/auth(auth)/router.decorator.js index 5b8fb0e6a..752ad0720 100644 --- a/templates/app/client/components/auth(auth)/router.decorator.js +++ b/templates/app/client/components/auth(auth)/router.decorator.js @@ -29,8 +29,8 @@ angular.module('<%= scriptAppName %>.auth') } event.preventDefault();<% if (filters.ngroute) { %> - $location.path('/');<% } if (filters.uirouter) { %> - $state.go('main');<% } %> + $location.path('/login');<% } if (filters.uirouter) { %> + $state.go('login');<% } %> }); } }); From 307898feea66958574fa859723e8a6cb27f233c8 Mon Sep 17 00:00:00 2001 From: mtvs Date: Mon, 25 Jul 2016 19:20:08 +0430 Subject: [PATCH 2/2] fix(client:auth): redirect a logged-in user when visits a guest page like /login or /signup --- .../app/account(auth)/account.routes.js | 6 ++- .../components/auth(auth)/router.decorator.js | 45 ++++++++++++------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/templates/app/client/app/account(auth)/account.routes.js b/templates/app/client/app/account(auth)/account.routes.js index d15b803c5..74c4f393c 100644 --- a/templates/app/client/app/account(auth)/account.routes.js +++ b/templates/app/client/app/account(auth)/account.routes.js @@ -8,7 +8,8 @@ export default function routes($stateProvider) { url: '/login', template: require('./login/login.<%= templateExt %>'), controller: 'LoginController', - controllerAs: 'vm' + controllerAs: 'vm', + guest: true }) .state('logout', { url: '/logout?referrer', @@ -27,7 +28,8 @@ export default function routes($stateProvider) { url: '/signup', template: require('./signup/signup.<%= templateExt %>'), controller: 'SignupController', - controllerAs: 'vm' + controllerAs: 'vm', + guest: true }) .state('settings', { url: '/settings', diff --git a/templates/app/client/components/auth(auth)/router.decorator.js b/templates/app/client/components/auth(auth)/router.decorator.js index ea205ec7d..1668069d0 100644 --- a/templates/app/client/components/auth(auth)/router.decorator.js +++ b/templates/app/client/components/auth(auth)/router.decorator.js @@ -5,35 +5,46 @@ export function routerDecorator($rootScope<% if(filters.ngroute) { %>, $location 'ngInject'; // Redirect to login if route requires auth and the user is not logged in, or doesn't have required role $rootScope.$on(<% if(filters.ngroute) { %>'$routeChangeStart'<% } %><% if(filters.uirouter) { %>'$stateChangeStart'<% } %>, function(event, next) { - if(!next.authenticate) { - return; - } + if(next.authenticate) { + if(typeof next.authenticate === 'string') { + Auth.hasRole(next.authenticate).then(has => { + if(has) { + return; + } - if(typeof next.authenticate === 'string') { - Auth.hasRole(next.authenticate).then(has => { - if(has) { - return; - } + event.preventDefault(); + return Auth.isLoggedIn().then(is => { + <%_ if(filters.ngroute) { _%> + $location.path(is ? '/' : '/login');<% } %> + <%_ if(filters.uirouter) { _%> + $state.go(is ? 'main' : 'login');<% } %> + }); + }); + } else { + Auth.isLoggedIn().then(is => { + if(is) { + return; + } - event.preventDefault(); - return Auth.isLoggedIn().then(is => { + event.preventDefault(); <%_ if(filters.ngroute) { _%> - $location.path(is ? '/' : '/login');<% } %> + $location.path('/login');<% } %> <%_ if(filters.uirouter) { _%> - $state.go(is ? 'main' : 'login');<% } %> + $state.go('login');<% } %> }); - }); - } else { + } + } + else if(next.guest) { Auth.isLoggedIn().then(is => { - if(is) { + if(!is) { return; } event.preventDefault(); <%_ if(filters.ngroute) { _%> - $location.path('/login');<% } %> + $location.path('/');<% } %> <%_ if(filters.uirouter) { _%> - $state.go('login');<% } %> + $state.go('main');<% } %> }); } });