Reputation: 153
I am new to Angular 2 Meteor. I am implementing a project in which I have to logout the user when he closes his window; and again, when he opens the app, he should see the login page.
I searched the internet but didn't find anything for Angular 2 Meteor.
https://github.com/mizzao/meteor-user-status
I found this but I don't know how to use it in Angular 2 case for logout user. where to put this code in server side in Angular 2 Meteor and how to logout the user.
Meteor.users.find({ "status.online": true }).observe({
added: function(id) {
},
removed: function(id) {
}
});
Can someone help?
Upvotes: 3
Views: 621
Reputation: 6147
I implemented this in one of my project you can logout from server side in angular 2 meteor using mizzao/meteor-user-status package. this is what you have to do
step 1) first of all install this package
meteor add mizzao:user-status
step 2) after installing this your users collection table show you some new entry with basic account info. now your json file have some extra keys
{
"_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": "[email protected]",
"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": []
}
}
step 3) user-status code on server side
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 });
}
});
});
step 4) in client side for login page just put this code
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]);
}
}
Upvotes: 1