gpt4 book ai didi

javascript - 如何从 Angular 2 Meteor 的服务器端检测和注销空闲用户?

转载 作者:太空狗 更新时间:2023-10-29 19:29:55 26 4
gpt4 key购买 nike

我是 Angular 2 Meteor 的新手。我正在实现一个项目,在该项目中,当用户关闭窗口时我必须注销用户;再次,当他打开应用程序时,他应该会看到登录页面。

我在互联网上进行了搜索,但没有找到任何关于 Angular 2 Meteor 的信息。

https://github.com/mizzao/meteor-user-status
我找到了这个,但我不知道如何在注销用户的 Angular 2 案例中使用它。将此代码放在 Angular 2 Meteor 的服务器端的什么位置以及如何注销用户。

Meteor.users.find({ "status.online": true }).observe({
added: function(id) {
},
removed: function(id) {
}
});

有人可以帮忙吗?

最佳答案

我在我的一个项目中实现了这个,你可以使用 mizzao/meteor-user-status 包在 angular 2 meteor 中从服务器端注销。这是你必须做的

步骤 1) 首先安装这个包

meteor add mizzao:user-status

第 2 步) 安装此后,您的用户集合表会显示一些包含基本帐户信息的新条目。现在你的 json 文件有一些额外的键

 {
"_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": []
}
}

步骤 3) 服务器端的用户状态代码

           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 });
}
});
});

步骤 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]);
}
}

关于javascript - 如何从 Angular 2 Meteor 的服务器端检测和注销空闲用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41646487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com