我正在使用PHP web api在angularjs中构建一个应用程序,用于获取运行所需的数据。我找到了一种很好地设置路线并登录用户的方法,但是注意到当他们在登录后点击后退按钮时会被注销。
(function () {
'use strict';
angular
.module('app', ['ngRoute', 'ngCookies'])
.config(config)
.run(run);
config.$inject = ['$routeProvider', '$locationProvider'];
function config($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
controller: 'HomeController',
templateUrl: 'pages/home.html',
controllerAs: 'vm'
})
.when('/about', {
controller: 'AboutController',
templateUrl: 'pages/about.html',
controllerAs: 'vm'
})
.when('/login', {
controller: 'LoginController',
templateUrl: 'pages/login.html',
controllerAs: 'vm'
})
.when('/register', {
controller: 'RegisterController',
templateUrl: 'pages/register.html',
controllerAs: 'vm'
})
.when('/calendar', {
controller: 'CalendarController',
templateUrl: 'pages/calendar.html',
controllerAs: 'vm'
})
.when('/newsfeed', {
controller: 'FeedController',
templateUrl: 'pages/newsfeed.html',
controllerAs: 'vm'
})
.when('/favorites', {
controller: 'FavoritesController',
templateUrl: 'pages/favorites.html',
controllerAs: 'vm'
})
.when('/chat', {
controller: 'ChatController',
templateUrl: 'pages/chat.html',
controllerAs: 'vm'
})
.when('/contact', {
controller: 'ContactController',
templateUrl: 'pages/contact.html',
controllerAs: 'vm'
})
.when('/settings', {
controller: 'SettingsController',
templateUrl: 'pages/settings.html',
controllerAs: 'vm'
})
.when('/personal', {
controller: 'ProfileController',
templateUrl: 'pages/personal.html',
controllerAs: 'vm'
})
.when('/stats', {
controller: 'StatsController',
templateUrl: 'pages/stats.html',
controllerAs: 'vm'
})
.when('/requests', {
controller: 'RequestController',
templateUrl: 'pages/requests.html',
controllerAs: 'vm'
})
.when('/change-password', {
controller: 'RegisterController',
templateUrl: 'pages/change-password.html',
controllerAs: 'vm'
})
.when('/weather', {
controller: 'WeatherController',
templateUrl: 'pages/weather.html',
controllerAs: 'vm'
})
.otherwise({ redirectTo: '/login' });
$locationProvider.html5Mode({
enabled: true,
requireBase: true
});
$locationProvider.hashPrefix('');
}
run.$inject = ['$rootScope', '$location', '$cookies', '$http'];
function run($rootScope, $location, $cookies, $http) {
// keep user logged in after page refresh
$rootScope.globals = $cookies.getObject('globals') || {};
if ($rootScope.globals.currentUser) {
$http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.authdata;
}
$rootScope.$on('$locationChangeStart', function (event, next, current) {
// redirect to login page if not logged in and trying to access a restricted page
var restrictedPage = $.inArray($location.path(), ['/login', '/register']) === -1;
var loggedIn = $rootScope.globals.currentUser;
if (restrictedPage && !loggedIn) {
$location.path('/');
}
});
}
function HeaderCtrl($scope) {
$scope.header = {name: "frame.html", url: "frame.html"};
}
})();
我尝试添加这个,就像我喜欢其他一些帖子一样没有运气:
$rootScope.$on('$stateChangeStart', function(event, toState, toParams,
fromState, fromParams){
var loggedIn = $rootScope.globals.currentUser;
//userIsLogged is a flag you should retrieve from your code
if(loggedIn && toState === 'login'){
event.preventDefault();
}
});
我也有一个身份验证服务,如果这会有所帮助,虽然它应该将cookie设置为一周。
非常感谢任何帮助,并提前感谢!
答案 0 :(得分:0)
我现在添加/编辑了以下代码段以解决此问题:
$rootScope.$on('$locationChangeStart', function (event, next, current) {
// redirect to login page if not logged in and trying to access a restricted page
var restrictedPage = $.inArray($location.path(), ['/login', '/register']) === -1;
var loggedIn = $rootScope.globals.currentUser;
if (restrictedPage && !loggedIn) {
$location.path('/login');
} else if ($location.path() === '/login' && loggedIn) {
$location.path('/');
}
});