If user visits the base url or '/' and they are *not logged in* to the
application I would like them to see the *'welcome' state*. However *if a
user is logged in* I would like them to see the *'home' state*. So a user
would be able to browse around the site but when ever they went to '/' they
will only see the state they have permission to see.
I am using the angular-fullstack generator with express, sockets & MongoDB
and Ui-Router
Here is how I currently have things:
'use strict';
angular.module('cbuiRouterApp', [
'ngCookies',
'ngResource',
'ngSanitize',
'ui.bootstrap',
'btford.socket-io',
'ui.router',
])
.config(function ($stateProvider, $urlRouterProvider, $locationProvider,
$httpProvider) {
$urlRouterProvider
.otherwise('/');
$stateProvider
.state('main', {
url: '/',
templateUrl: 'app/main/main.html',
controller: 'MainCtrl',
title: 'Main',
mainClass: 'main'
})
.state('welcome', {
url: '/',
templateUrl: 'app/welcome/welcome.html',
controller: 'WelcomeCtrl',
title: 'Welcome',
mainClass: 'welcome'
})
.state('login', {
url: '/login',
templateUrl: 'app/account/login/login.html',
controller: 'LoginCtrl'
});
$locationProvider.html5Mode(true);
$httpProvider.interceptors.push('authInterceptor');
})
.factory('authInterceptor', function ($rootScope, $q, $cookieStore,
$location) {
return {
// Add authorization token to headers
request: function (config) {
config.headers = config.headers || {};
if ($cookieStore.get('token')) {
config.headers.Authorization = 'Bearer ' + $cookieStore.get(
'token');
}
return config;
},
// Intercept 401s and redirect you to login
responseError: function(response) {
if(response.status === 401) {
$location.path('/login');
// remove any stale tokens
$cookieStore.remove('token');
return $q.reject(response);
}
else {
return $q.reject(response);
}
}
};
})
.run(function ($rootScope, $location, Auth) {
// Redirect to login if route requires auth and you're not logged in
$rootScope.$on('$stateChangeStart', function (event, next) {
if (next.authenticate && !Auth.isLoggedIn()) {
$location.path('/login');
}
});
});
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.