I can think in two ways to do this. The first one is not display the chat to guests.
In your HTML file you can use:
{{#if currentUser}}
<!-- The code to show the chat -->
{{else}}
<!-- The code if is not logged in -->
You must login to use the chat
{{/if}}
Refered Docs from here
But you may let the guest try to enter a comment, to block him you can do this:
// when Send Chat clicked at the message to the collection
Template.chatBox.events({
"click #send": function() {
if (Meteor.user() == null) {
alert("You must login to post");
return;
}
$('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
var message = $('#chat-message').val();
chatCollection.insert({
userId: 'me',
message: message
});
$('#chat-message').val('');
//add the message to the stream
chatStream.emit('chat', message);
},
"keypress #chat-message": function(e) {
if (Meteor.user() == null) {
alert("You must login to post");
return;
}
if (e.which == 13) {
$('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
console.log("you pressed enter");
e.preventDefault();
//repeat function from #send click event here
var message = $('#chat-message').val();
chatCollection.insert({
userId: 'me',
message: message
});
$('#chat-message').val('');
//add the message to the stream
chatStream.emit('chat', message);
}
}
});
Both are probably not the more user friendly way to do this, but they show how you can handle if the user is logged or not.
A good tutorial that will show you step by step how to do a chat is this