Разрешить чат только зарегистрированным пользователям? Приложение Meteor Chat
Я работаю над приложением чата, используя meteor с этим учебным пособием, и я хочу, чтобы оно было, поэтому вам необходимо войти в систему, прежде чем вы сможете добавлять комментарии. Причина, по которой я это делаю, заключается в том, что я хочу, чтобы все на моем сайте имели псевдоним вместо имени, например anonymous385hg83f или что-то еще. В учебнике используется account-ui, а вот код для приложения чата:
JavaScript:
// get the value for handlerbar helper user
Template.chatMessage.helpers({
"user": function() {
if(this.userId == 'me') {
return this.userId;
} else if(this.userId) {
getUsername(this.userId);
return Session.get('user-' + this.userId);
} else {
return 'anonymous-' + this.subscriptionId;
}
}
});
// when Send Chat clicked at the message to the collection
Template.chatBox.events({
"click #send": function() {
$('#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 (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);
}
}
});
chatStream.on('chat', function(message) {
chatCollection.insert({
userId: this.userId,
subscriptionId: this.subscriptionId,
message: message
});
});
Я понятия не имею, как это сделать. Кто-нибудь знает, как сделать так, чтобы пользователь должен был зарегистрироваться / войти, прежде чем они смогут общаться?
1 ответ
Я могу думать двумя способами, чтобы сделать это. Первый не отображать чат для гостей.
В вашем HTML-файле вы можете использовать:
{{#if currentUser}}
<!-- The code to show the chat -->
{{else}}
<!-- The code if is not logged in -->
You must login to use the chat
{{/if}}
Справочные документы отсюда
Но вы можете позволить гостю попытаться ввести комментарий, чтобы заблокировать его, вы можете сделать это:
// 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);
}
}
});
Оба, вероятно, не самый удобный способ сделать это, но они показывают, как вы можете справиться, если пользователь вошел в систему или нет.
Хороший учебник, который покажет вам шаг за шагом, как сделать чат , это