{{#if currentUser}} is not logged in -> insert to database


#1

Hello!

I’m trying to create a list that displays currently logged in users. But im struggling on the “when a user signs out / exists the page” part.

Can i insert code somehow like this?

{{#if currentUser}}
    UsersList.insert({
        user: Meteor.user().username,
        timeJoined: Session.get('time')
    })
{{else}}
    UsersList.delete({
        //something something
    })
{{/if}}

EDIT:

Template.online.helpers({
    'isOnline': function(){
        if(Meteor.user().username){
            alert('signed in');
        } else {
            alert('not signed in');
        }
        if(!Meteor.user().username){
            alert('not signed in');
        }
    }   
});

“Signed in” pops up. “Not signed in” never happens though.

Any suggestion?


#2

Have you tried using this package?

Keeps track of user connection data, such as IP addresses, user agents, and client-side activity, and tracks this information in Meteor.users as well as some other objects. This allows you to easily see users that are online, for applications such as rendering the users box below showing online users in green and idle users in orange.

Also bare in mind that you cannot execute raw js-code in your blaze templates.


#3

I think you are looking for something like this:

Tracker.autorun(function () {
    if (Meteor.userId()) {
        alert('signed in');
    } else {
        alert('signed out');
    }
});

#4

That seems promising, i simply want to print out a list of online users by username (accounts-password package)

However, i cant seem to get it to work. I dont think im doing it right. Can you explain?


#5

That didnt seem to work at all, just giving an error. 'Too many “)” '. But i cant find any overflowing “)”


#6

Sorry, I haven’t slept. I forgot the function (), edited it.


#7

Ah cool, works now! The “meteor-user-status” package seems to be able to solve my problem more easily tough. Do you know how it works?


#8

Yep, the package is much more robust. The Readme on github is pretty comprehensive and walks you through setting it up, so just have a read of it.


#9

Yes i’ve read it and tried the examples but i cant seem to get it to work anyway. What am i supposed to do in the template? in the view?


#10

So from the Readme, if you have:

Template.foo.usersOnline = function() {
  return Meteor.users.find({ "status.online": true });
};

You should be able to call usersOnline in your template like this:

<template name="foo">
    <ul>
        {{#each usersOnline}}
            <li>
                {{username}}
            </li>
        {{/each}}
    </ul>
</template>

#11

Thanks but it doesnt work for me, nothing simply comes up. the foo list remains empty.

Should i put the Template.foo.usersOnline in the server? When i do, an error comes up regarding autopublish. Do i need to remove that?

Sorry for not understanding, im new to meteor


#12

If you can upload your project to github, I’ll try to help you.


#13

Might want to take a look at this: https://github.com/dburles/meteor-presence