我在我的一个项目中实现了这一点,您可以使用 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]);
}
}