Javascript 如何在Angular 2 Meteor中从服务器端检测并注销空闲用户?
我是角2流星的新手。我正在实施一个项目,在该项目中,当用户关闭窗口时,我必须注销该用户;同样,当他打开应用程序时,他应该会看到登录页面 我在网上搜索了一下,但没有找到任何关于Angular 2 Meteor的信息Javascript 如何在Angular 2 Meteor中从服务器端检测并注销空闲用户?,javascript,angular,meteor,angular2-meteor,Javascript,Angular,Meteor,Angular2 Meteor,我是角2流星的新手。我正在实施一个项目,在该项目中,当用户关闭窗口时,我必须注销该用户;同样,当他打开应用程序时,他应该会看到登录页面 我在网上搜索了一下,但没有找到任何关于Angular 2 Meteor的信息 我发现了这个,但我不知道如何在Angular 2中为注销用户使用它。在Angular 2 Meteor中,将此代码放在服务器端的何处以及如何注销用户 Meteor.users.find({ "status.online": true }).observe({ added: fun
我发现了这个,但我不知道如何在Angular 2中为注销用户使用它。在Angular 2 Meteor中,将此代码放在服务器端的何处以及如何注销用户
Meteor.users.find({ "status.online": true }).observe({
added: function(id) {
},
removed: function(id) {
}
});
有人能帮忙吗?我在我的一个项目中实现了这一点,您可以使用mizzao/meteor用户状态包从angular 2 meteor的服务器端注销。这就是你必须要做的 步骤1)首先安装此软件包 第2步)安装此工具后,您的用户收集表将显示一些包含基本帐户信息的新条目。现在,json文件有了一些额外的键 步骤3)服务器端的用户状态代码 步骤4)在登录页面的客户端,只需输入以下代码
ngOnInit() {
if (Meteor.user()) { <-- make sure you use Meteor.user() only . if you use Meteor.userId then it can create some issue because it is stored on localhost but Meteor.user() everytime calls server for user data. choice is yours.
this._router.navigate([//your routename]);
}
}
ngOnInit(){
if(Meteor.user()){
{
"_id": "uxuhCgmCg6wkK795a",
"createdAt": {
"$date": "2016-09-30T05:54:07.414Z"
},
"services": {
"password": {
"bcrypt": "$2a$10$AxCqCcNsZzdtHSxB9ap9t.KY9kjV2E/U0woF4SFPRBqUD8Bj0XpuO"
},
"resume": {
"loginTokens": [{
"when": {
"$date": "2017-01-09T05:50:17.784Z"
},
"hashedToken": "XHpxCKS/kUALKyXCANDBHrJXRV9LAsmCBOOWwmUhAaU="
}]
}
},
"username": "jhon",
"emails": [{
"address": "jhon@gmail.com",
"verified": false
}],
"status": {
"online": true,
"lastLogin": {
"date": {
"$date": "2017-01-09T05:50:19.055Z"
},
"ipAddr": "127.0.0.1",
"userAgent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36"
},
"idle": false
},
"resume": {
"loginTokens": []
}
}
import { Meteor } from 'meteor/meteor';
Meteor.startup(() => {
// load initial Parties
Meteor.users.find({
"status.online": true
}).observe({
added: function(id: any) {
// id just came online
console.log("--------- New User Login ---------");
console.log("user " + id.username + " (" + id._id + ") is online now");
},
removed: function(id: any) {
// id just went offline
console.log("----------- User idle --------------");
console.log("user " + id.username + " (" + id._id + ") is gone offline");
// ** use this mongodb query to remove user who go offline from server side
Meteor.users.update({_id: id._id }, {$set: {"services.resume.loginTokens": []} }, { multi: true });
}
});
});
ngOnInit() {
if (Meteor.user()) { <-- make sure you use Meteor.user() only . if you use Meteor.userId then it can create some issue because it is stored on localhost but Meteor.user() everytime calls server for user data. choice is yours.
this._router.navigate([//your routename]);
}
}