仅在 MEAN js 中对经过身份验证的用户的页面查看权限
Page view permission to authenticated users only in MEAN js
我正在查看 MEAN js,我刚刚开始他们的教程,其中包含用户和文章。
注册后我可以正确地创建文章,很好。
如果我注销,如果我尝试创建文章返回 /article 页面,它会将我正确重定向到登录页面,但是,如果用户未登录,我如何才能阻止查看该页面?
代码没有什么特别之处,就是原来的MEAN app:http://meanjs.org/
这是文章的路由:
'use strict';
// Setting up route
angular.module('articles').config(['$stateProvider',
function($stateProvider) {
// Articles state routing
$stateProvider.
state('app.listArticles', {
url: '/articles',
templateUrl: 'modules/articles/views/list-articles.client.view.html'
}).
state('app.createArticle', {
url: '/articles/create',
templateUrl: 'modules/articles/views/create-article.client.view.html'
}).
state('app.viewArticle', {
url: '/articles/:articleId',
templateUrl: 'modules/articles/views/view-article.client.view.html',
controller: 'ArticlesController'
}).
state('app.editArticle', {
url: '/articles/:articleId/edit',
templateUrl: 'modules/articles/views/edit-article.client.view.html'
});
}
]);
目前我自己找到的唯一解决方案是在文章控制器中添加以下代码:
if ($scope.authentication.user === "") {
$location.path('home/');
}
但我认为这不是最好的解决方案。
您可以对状态使用解析方法,并在用户每次访问时检查他们是否已获得授权。不确定 ui-router 是否包含 resolve。
例如
.state( "app.profile", {
templateUrl: "/build/templates/profile.html",
controller: "ProfileCtrl",
resolve: requireUser } )
var requireUser = { User: [ "$location", "$rootScope", "$q", "API", "Session",
function ( $location, $rootScope, $q, API, Session )
{
var deferred = $q.defer();
if( Session.get.authToken() )
{
if( Session.get.user() )
{
deferred.resolve( $rootScope.user );
}
else
{
$location.path( "/login" );
}
}
else
{
deferred.reject( "No user token" );
$location.path( "/login" );
}
return deferred.promise;
} ] };
我在看一个类似的问题,我想只为经过身份验证的用户打开某些路由,并阻止其他人访问它们。
我创建了一个名为 AuthenticateController 的新控制器:
angular.module('projects').controller('AuthenticateController', ['$scope', '$stateParams', '$location', 'Authentication', 'Projects',
function($scope, $stateParams, $location, Authentication, Projects) {
$scope.authentication = Authentication;
if ($scope.authentication.user === '') {
$location.path('home/');
}
}
并将其添加到该模块的 controller.js 文件中。
这有一些(非常)基本的代码,用于检查用户是否经过身份验证,如果没有,则将他们返回主页。然后我将路由绑定到 routes.js:
中的这个控制器
state('createProject', {
url: '/projects/create',
templateUrl: 'modules/projects/views/create-project.client.view.html',
controller: 'AuthenticateController'
}).
这个解决方案似乎可行,但我是 MEAN 的新手(我的第一周),所以如果有人有更好的解决方案或可以详细说明,那么不仅欢迎而且鼓励建议。
你为什么不在 header.client.view.html 文件的导航中使用 ng-show="authentication.user"!
我正在查看 MEAN js,我刚刚开始他们的教程,其中包含用户和文章。
注册后我可以正确地创建文章,很好。
如果我注销,如果我尝试创建文章返回 /article 页面,它会将我正确重定向到登录页面,但是,如果用户未登录,我如何才能阻止查看该页面?
代码没有什么特别之处,就是原来的MEAN app:http://meanjs.org/
这是文章的路由:
'use strict';
// Setting up route
angular.module('articles').config(['$stateProvider',
function($stateProvider) {
// Articles state routing
$stateProvider.
state('app.listArticles', {
url: '/articles',
templateUrl: 'modules/articles/views/list-articles.client.view.html'
}).
state('app.createArticle', {
url: '/articles/create',
templateUrl: 'modules/articles/views/create-article.client.view.html'
}).
state('app.viewArticle', {
url: '/articles/:articleId',
templateUrl: 'modules/articles/views/view-article.client.view.html',
controller: 'ArticlesController'
}).
state('app.editArticle', {
url: '/articles/:articleId/edit',
templateUrl: 'modules/articles/views/edit-article.client.view.html'
});
}
]);
目前我自己找到的唯一解决方案是在文章控制器中添加以下代码:
if ($scope.authentication.user === "") {
$location.path('home/');
}
但我认为这不是最好的解决方案。
您可以对状态使用解析方法,并在用户每次访问时检查他们是否已获得授权。不确定 ui-router 是否包含 resolve。
例如
.state( "app.profile", {
templateUrl: "/build/templates/profile.html",
controller: "ProfileCtrl",
resolve: requireUser } )
var requireUser = { User: [ "$location", "$rootScope", "$q", "API", "Session",
function ( $location, $rootScope, $q, API, Session )
{
var deferred = $q.defer();
if( Session.get.authToken() )
{
if( Session.get.user() )
{
deferred.resolve( $rootScope.user );
}
else
{
$location.path( "/login" );
}
}
else
{
deferred.reject( "No user token" );
$location.path( "/login" );
}
return deferred.promise;
} ] };
我在看一个类似的问题,我想只为经过身份验证的用户打开某些路由,并阻止其他人访问它们。
我创建了一个名为 AuthenticateController 的新控制器:
angular.module('projects').controller('AuthenticateController', ['$scope', '$stateParams', '$location', 'Authentication', 'Projects',
function($scope, $stateParams, $location, Authentication, Projects) {
$scope.authentication = Authentication;
if ($scope.authentication.user === '') {
$location.path('home/');
}
}
并将其添加到该模块的 controller.js 文件中。
这有一些(非常)基本的代码,用于检查用户是否经过身份验证,如果没有,则将他们返回主页。然后我将路由绑定到 routes.js:
中的这个控制器 state('createProject', {
url: '/projects/create',
templateUrl: 'modules/projects/views/create-project.client.view.html',
controller: 'AuthenticateController'
}).
这个解决方案似乎可行,但我是 MEAN 的新手(我的第一周),所以如果有人有更好的解决方案或可以详细说明,那么不仅欢迎而且鼓励建议。
你为什么不在 header.client.view.html 文件的导航中使用 ng-show="authentication.user"!