我是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) {
}
});
有人可以帮忙吗?
答案 0 :(得分:1)
我在我的一个项目中实现了这个,您可以使用mizzao / meteor-user-status包从角度2流星的服务器端注销。这就是你要做的事情
步骤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]);
}
}