我试图阻止打开多个浏览器的用户登录。
当用户注册时,我有一个Meteor.user()
对象填充如下:
{
"_id" : "uSS2RqZnnFwui67wk",
"createdAt" : ISODate("2017-05-15T07:28:10.546Z"),
"services" : {
"password" : {
"bcrypt" : "$2a$10$DPgA59Gmob4ajzjYZyh5auoHRUyQuF1/7M0KaWz.nzW0mIEqzlDK6"
},
"resume" : {
"loginTokens" : [
{
"when" : ISODate("2017-05-15T13:42:29.322Z"),
"hashedToken" : "tkoQnweSQhgRKGzaJTAkUU3/Ljd3p4wrBJfrRvRRlcY="
}
]
}
},
"username" : "johndoe",
"emails" : [
{
"address" : "lkj@gmail.com",
"verified" : false
}
],
"profile" : {
"name" : "John Doe",
"mobile" : "9637637941",
"email" : "lkj@gmail.com",
"address" : "kfasd, asdas,d as dsad",
"gender" : "M",
"state" : "Uttar Pradesh",
"customerType" : "CLIENT",
"isBlocked" : true
},
"status" : {
"online" : true,
"lastLogin" : {
"date" : ISODate("2017-05-15T14:12:02.094Z"),
"ipAddr" : "127.0.0.1",
"userAgent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"
},
"idle" : false
}
}
参考上面的代码,我尝试根据user.profile.isBlocked*
状态更新用户界面。
我的 UI.html 如下:
<template name="App_watch">
{{#if isBlocked}}
User Has been Blocked.
{{else}}
User has Access.
{{/if}}
</template>
我的 UI.js 如下:
import { Meteor } from 'meteor/meteor';
import './UI.html';
Template.App_watch.helpers({
isBlocked() {
user = Meteor.users.find({_id: Meteor.userId});
return user.profile.isBlocked;
}
});
在下面的代码中,我只是监控是否有多个浏览器以相同的登录方式打开。如果是,则阻止用户,否则取消阻止用户。
import './fixtures.js';
import './register-api.js';
UserStatus.events.on("connectionLogin", function(fields) {
var count = UserStatus.connections.find({userId : fields.userId}).count();
if(count > 1) { //Block
Meteor.users.update({_id: Meteor.userId()}, {$set: {"profile.isBlocked": true}});
} else { // Unblock
Meteor.users.update({_id: Meteor.userId()}, {$set: {"profile.isBlocked": false}});
}
});
问题陈述:
我希望将isBlocked变量设置为响应,因为isBlocked标志会为用户更改。目前它是静态的,需要刷新。
答案 0 :(得分:3)
尝试:
Template.App_watch.helpers({
isBlocked() {
return Meteor.user() && Meteor.user().profile && Meteor.user().profile.isBlocked;
}
});
如果您要查找单个对象,则需要使用.findOne()
而不是.find()
,因为后者会返回光标。它还Meteor.userId()
而非Meteor.userId