用户登录/
路径。
如何引导他们远离我的应用的/
视图
/packages/system/public/views/index.html
到另一个视图/app
?
/packages/system/public/views/app.html
我希望此视图/app
是安全的,因此只有登录用户才能访问它。未登录的用户应该发送回/
。
答案 0 :(得分:1)
在/packages/users/controllers/meanUser.js
// Register the login() function
$scope.login = function() {
$http.post('/login', {
email: $scope.user.email,
password: $scope.user.password
})
.success(function(response) {
// authentication OK
$scope.loginError = 0;
$rootScope.user = response.user;
$rootScope.$emit('loggedin');
if (response.redirect) {
if (window.location.href === response.redirect) {
//This is so an admin user will get full admin page
window.location.reload();
} else {
window.location = response.redirect;
}
} else {
// Redirect Here
$location.url('/');
$location.url('/articles'); // Will take you to the articles view after login
}
})
.error(function() {
$scope.loginerror = 'Authentication failed.';
});
};
如果您在未登录时尝试访问安全路线时需要将用户重定向到其他页面,可以参考/packages/articles/public/routes/articles.js中的代码
// This function checks if the user is logged in and redirects to the login page.
var checkLoggedin = function($q, $timeout, $http, $location) {
// Initialize a new promise
var deferred = $q.defer();
// Make an AJAX call to check if the user is logged in
$http.get('/loggedin').success(function(user) {
// Authenticated
if (user !== '0') $timeout(deferred.resolve);
// Not Authenticated
else {
$timeout(deferred.reject);
$location.url('/login');
}
});
return deferred.promise;
};
// These are your defined routes.
$stateProvider
.state('all articles', {
url: '/articles',
templateUrl: 'articles/views/list.html',
// This resolve runs the checkLoggedin function as the route is accessed
// and redirects if the user isn't logged in.
resolve: {
loggedin: checkLoggedin
}
});