使用 $rootScope.$on('$routeChangeStart', function(event, next, current),如果路由需要身份验证,我将重定向到登录页面。效果很好。
登录后如何重定向回预期路线?
使用 $rootScope.$on('$routeChangeStart', function(event, next, current),如果路由需要身份验证,我将重定向到登录页面。效果很好。
登录后如何重定向回预期路线?
这是对我有用的简化版本:
app = angular.module('ngApp', []).config(function ($routeProvider) {
$routeProvider
.when('/dashboard', {
templateUrl: 'dashboard.html',
controller: 'dashboardController',
loginRequired: true //
})
.when('/login', {
templateUrl: 'login.html',
controller: 'loginController'
})
.otherwise({redirectTo: '/login'})
});
然后在应用程序的运行块中:
app.run(function ($location, $rootScope) {
var postLogInRoute;
$rootScope.$on('$routeChangeStart', function (event, nextRoute, currentRoute) {
//if login required and you're logged out, capture the current path
if (nextRoute.loginRequired && Account.loggedOut()) {
postLogInRoute = $location.path();
$location.path('/login').replace();
} else if (postLogInRoute && Account.loggedIn()) {
//once logged in, redirect to the last route and reset it
$location.path(postLogInRoute).replace();
postLogInRoute = null;
}
});
});
这是我如何做的一个例子,希望它有帮助:
在路由提供者上,首先以某种方式设置公共访问:
// Just a demo on how the routes were set up to determine public access
angular.module('ngApp', []).config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller : 'MainController',
})
.when('/login', {
templateUrl : 'views/login.html',
controller : 'LoginController',
publicAccess : true // This is used in $routeChangeStart later
});
});
});
然后:
$rootScope.$on('$routeChangeStart', function(event, next, current) {
var publicAccess = next.publicAccess || false;
// This is just a service I made, this is how I check logged in status
// AuthenticationService.check() returns a promise
AuthenticationService.check().then(function() {
// As this is a promise, this block signals that the user is logged in
// If the page is marked as public access, then redirect to private area
if (publicAccess)
$location.path('/').replace();
}, function() {
// Since this segment of the promise signals that the user is not
// logged in, if the page is not publicly accessible, redirect to login
if (!publicAccess)
$location.path('/login').replace();
});
});
以下答案来自OP。
这是我解决它的方法:
我将此事件广播添加到 $routeChangeStart
$rootScope.$on('$routeChangeStart', function(event, next, current) {
if (next.authRequired) {
var deferred = $q.defer(),
_token = mainConfig.csrfToken;
security.getCurrentUser(true, _token).success(function (data, status, headers, config) {
if(status == 200) {
// set the local scope variables
next.scope.isAuthenticated = true;
next.scope.user = security.currentUser;
// Broadcast out to each of the listeners
$rootScope.$broadcast('currentUserAuthenticated');
// Any other response requires a signin. Redirect.
} else {
next.scope.isAuthenticated = false;
next.scope.user = null;
$rootScope.$broadcast('authenticationRequired', $location.url());
$location.path('/signin');
}
});
}
});
然后在我的安全工厂中,我监听了事件并存储了位置,如下所示:
$rootScope.$on('authenticationRequired', function(event, callingRoute) {
redirectRoute = callingRoute;
});