阻止访问 Meteor 中的页面
Preventing access to a page in Meteor
我正在 meteor 中构建一个应用程序,其中一个页面只有在用户登录后才对用户可见。页面的 link 在导航 header 中,我希望在用户单击 link 而不登录时显示登录对话框。这是显示对话框的代码:
<template name="header">
<a href="#" id="createPost">Create Post</a>
</template>
Template.header.events({
"click #createPost": function (evt) {
evt.preventDefault();
if(!Meteor.user()) {
$('#myModal').modal("show"); //bootstrap modal dialog
}else{
Router.go('/createPost');
}
}
}
但是,问题是 Meteor.user() 检查可以很容易地从浏览器控制台使用 Meteor.user = function(){return true;}
绕过
我尝试检查路由中的 Meteor.user() 并抛出如下异常:
Router.route('/createPost', function () {
if (!Meteor.user()) {
throw new Meteor.Error(500, 'You are not logged in.');
}
this.render('newbag');
});
但是一旦在浏览器中修改了 Meteor.user,此检查也将不起作用。
处理这种情况并防止页面显示的最佳方法是什么。
无法确保客户端不会看到给定页面。
即使您最终采取了很多技巧,客户仍会收到所有模板并且他仍然可以访问它,无论是使用他的浏览器控制台还是通过更高级的技巧。
您想要的是防止用户查看和操作 数据,这是一种必须在服务器端进行安全验证,并且可以在客户端进行的验证给用户更好的感觉。
例如,在出版物中:
Meteor.publish('userData', function() {
if(!this.userId) {
throw new Meteor.Error('user not logged-in');
}
//...
});
您必须确保在正常、合法地使用您的应用程序时,一切都运行良好,客户看到他能看到的,并得到他需要的提示得到提示(这里,"Please log in")。
如果客户想搞砸,就让他把页面弄坏吧。
我同意其他答案:如果你真的想锁定一个应用程序,那么你也需要考虑服务器端控制。
对于安全性要求较低的应用程序,我通常会使用 iron-router
:
执行与您的代码类似的操作
Router.route('/', {
name: 'home',
template: 'home',
onBeforeAction: function(){
var currentUser = Meteor.userId();
if(currentUser){
this.next();
} else {
this.render("login");
}
}
});
我在控制台上测试了你的 hack,它可靠地不断重定向到 'login',但登录屏幕本身(我使用的是 accounts-ui
和 accounts-password
)随后中断,因为您无法注销 Null 用户。
我正在 meteor 中构建一个应用程序,其中一个页面只有在用户登录后才对用户可见。页面的 link 在导航 header 中,我希望在用户单击 link 而不登录时显示登录对话框。这是显示对话框的代码:
<template name="header">
<a href="#" id="createPost">Create Post</a>
</template>
Template.header.events({
"click #createPost": function (evt) {
evt.preventDefault();
if(!Meteor.user()) {
$('#myModal').modal("show"); //bootstrap modal dialog
}else{
Router.go('/createPost');
}
}
}
但是,问题是 Meteor.user() 检查可以很容易地从浏览器控制台使用 Meteor.user = function(){return true;}
我尝试检查路由中的 Meteor.user() 并抛出如下异常:
Router.route('/createPost', function () {
if (!Meteor.user()) {
throw new Meteor.Error(500, 'You are not logged in.');
}
this.render('newbag');
});
但是一旦在浏览器中修改了 Meteor.user,此检查也将不起作用。 处理这种情况并防止页面显示的最佳方法是什么。
无法确保客户端不会看到给定页面。
即使您最终采取了很多技巧,客户仍会收到所有模板并且他仍然可以访问它,无论是使用他的浏览器控制台还是通过更高级的技巧。
您想要的是防止用户查看和操作 数据,这是一种必须在服务器端进行安全验证,并且可以在客户端进行的验证给用户更好的感觉。
例如,在出版物中:
Meteor.publish('userData', function() {
if(!this.userId) {
throw new Meteor.Error('user not logged-in');
}
//...
});
您必须确保在正常、合法地使用您的应用程序时,一切都运行良好,客户看到他能看到的,并得到他需要的提示得到提示(这里,"Please log in")。
如果客户想搞砸,就让他把页面弄坏吧。
我同意其他答案:如果你真的想锁定一个应用程序,那么你也需要考虑服务器端控制。
对于安全性要求较低的应用程序,我通常会使用 iron-router
:
Router.route('/', {
name: 'home',
template: 'home',
onBeforeAction: function(){
var currentUser = Meteor.userId();
if(currentUser){
this.next();
} else {
this.render("login");
}
}
});
我在控制台上测试了你的 hack,它可靠地不断重定向到 'login',但登录屏幕本身(我使用的是 accounts-ui
和 accounts-password
)随后中断,因为您无法注销 Null 用户。