【问题标题】:How to detect and logout idle user from server side in Angular 2 Meteor?如何在 Angular 2 Meteor 中从服务器端检测和注销空闲用户?
【发布时间】:2017-05-29 12:38:12
【问题描述】:

我是 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) {
  }
});

有人可以帮忙吗?

【问题讨论】:

    标签: javascript angular meteor angular2-meteor


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-25
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      相关资源
      最近更新 更多