Sending email after creating an entry in SailsJS - javascript

I am trying to send an email from Sails after creating a user but I don't know what callback to use and can't seem to find anything in the documentation.
I reckon there should be some function which can be attached to the model like so:
afterCreate: function(user) {
// do stuff with email here
}
but that doesn't work for me

Docs about the lifecycle callbacks available for models are here. You'll want to define an afterCreate method in your User model. For sending the email, use something like the nodemailer package.

Related

How do I get the data published at client-side's subscribe method on Meteor?

I want to send a message from Server to client to specific "channels"/"subjects".
So I thought the right way of doing it was using the Publish/Subscribe feature.
But there's something I'm missing:
How do I handle the messages sent by the server? All the examples are like this in client-side:
Meteor.subscribe('todos.inList', list._id);
What is this method doing? How do I get the data published?
How do I handle the messages sent by the server?
Subscribe to the publication(explaining later) and you will get access to the collection that are published by the server. Then you can execute queries on those collections.
How do I get the data published?
You can publish one or more collections to with Publication. You can publish the todo list of any given user by following publication:
Meteor.publish('users.todoList', function (userId) {
return TodoList.find({'userId': userId});
}
In addition to the answer above.another approach is to use MeteorObservable
https://angular-meteor.com/api/meteor-rxjs/latest/MeteorObservable

How to retrieve multiple users data based on userid in meteor js?

I have created a chat message functionality. The message stores the userid of sender and receiver. How can I get other details of the user just based on the userid?
Is creating a Meteor method the best way or I have to create publish/subscribe pattern for it.
I want to avoid sending all users data to client side.
I tried creating a meteor method but I don't think its the best approach.
Meteor.methods({
getUserInfoById: function (usrId) {
//console.log("BEGIN");
//console.log(usrId);
var user = Meteor.users.findOne(usrId);
//console.log(user);
//console.log("END");
return user;
}
});
Create a publisher (and also a subscriber) for the data about other users that you want a given user to see. You can restrict the fields returned using the {fields: ...} qualifier in the query.
This is a very common pattern. Using a method for this is an anti-pattern due to the extra round-trip delay for something you're likely to show quite often, i.e. another user's username or avatar.

How to use AutoForm to register users and login?

I want to use AutoForm to create a register form which subsequetnly creates users. Furtehremore, new users should be signed in automatically after registering.
Therefore, I created an AutoForm and supplied a meteormethod called signUp:
Meteor.methods({
signUp: function (doc) {
check(doc, Schema.signUp);
Accounts.createUser({username: doc.username, password: doc.password});
}
});
This works perfect. However, I don't know how I can login users from server side? Is this even possible?
If not, how can I solve this issue? Do I need to include Accounts.createUser({username: doc.username, password: doc.password}); in my Schema custom validation function?
To use aldeed:autoform with any collection, you first need to define schema to that collection because autoform relies on simple-schema attached to that collection. Without schema, the form won't appear and you'll see an uncaught exception thrown by autoform.
So at first, you need to define schema. After that, it's possible to update or insert users into Meteor.users collection. See the official docs for the typical plain-object structure of a users collection entity, at first you may just console.log currently authenticated Meteor.user() to see what required fields it has.
My personal advice would be to not use autoform to mess around with users. It's not very secure and you need to explicitly control what users (or roles) can CRUD your users and what users do not. That's just extra pain to solve, and it may simply negate the convenience of autoform.

How can I access controllers in EmberJS from different classes (like authenticators)?

I'm using an app generated using ember-cli with simple-auth and I need to access controllers from an authenticator.
You don't want to pass your logged in user data to a controller. Chances are that user data will be used in multiple routes/controllers, so you need a globally available object (Ember Services can solve that use case). Luckily this is already handled by ember simple auth in its session object, that is where you want to store user data.
In any controller you should be able to do something like:
this.get('session.secure.email'); // version 0.8.X
Read about the session here: https://github.com/simplabs/ember-simple-auth#the-session That is what you want to work with.
If you really want to access some controller within an authenticator, you can use the container from within the authenticator's methods, like so:
SimpleAuth.Authenticators.Base.extend({
authenticate: function() {
var controller = this.container.lookup('controller:yourControllerName');
// rest of authentication logic
}
});
But as mentioned, it's better to save the data in the session or even in the store (if you're using ember-data) and retrieve the data from that resource.

How to post a tweet with Meteor.js, Twitter and Oauth

i have a little problem with Meteor and Twitter.
All i want to do is posting a tweet through a click on a button. For this I have to authenticate myself over Oauth to the Twitterservice.
At the moment i am doing the authentification in a really complicated way springing from client to server and back. But now I found the function Meteor.loginWithTwitter. Originally I thought this function is only for logging you into your own application with the Twitterkeys, now i am not so sure anymore. Probably I can also use it for my problem. Because it seems that the Oauth-Process is completely (and in a simple way) implemented in Meteor.
Sadly i cann't find any documentation or examples for just logging in and getting the final oauth_token. And so all i got from Meteor back then i try the following code, is this errormessage:
Erromessage: Accounts.ConfigError {message: "Service not configured"}
Meteor.loginWithTwitter( function(err){
if (err){
console.log(err)
}else{
console.log("yeah");
}
});
I know i have to enter somewhere my Appinformation like the Consumer key, but i have no idea where. Can someone help me out and knows some examples for me? Or knows if i am even on the right track?
Thanks and greetings
Philipp
The easiest way of doing this: Add the accounts-ui package:
meteor add accounts-ui accounts-twitter
and in your template do
{{loginButtons}}
On the first start of the application, a click on the login button will guide you through the setup process. You will create a Twitter application and copy the consumer key and consumer secret into a form, that meteor presents you. Afterwards you can log in using Twitter.
Make sure to use the latest Meteor version (0.5.2 at this moment)
You can also config your consumer key and secret with code, this is an example with weibo but its work for twitter, google etc... (server side) :
// first, remove configuration entry in case service is already configured
Accounts.loginServiceConfiguration.remove({
service: "weibo"
});
Accounts.loginServiceConfiguration.insert({
service: "weibo",
clientId: "1292962797",
secret: "75a730b58f5691de5522789070c319bc"
});
You need to add what #Tom31 suggested in your server side, i.e., I have a /server/server.js
Accounts.loginServiceConfiguration.remove({"service": "twitter"});
Accounts.loginServiceConfiguration.insert({
"service": "twitter",
"consumerKey" : "<yours>",
"secret" : "<yours>"
});
Finally, your access token are stored in your user at the database but this information it is not propagated to the client and, if you want to have access to it, you new to create a server side method and access it through Meteor.call or Meteor.apply
Updated: Example of my server side method
I've created my server side method like this:
Meteor.methods({
...
userGet: function(id) {
return removeSensibleFields( Meteor.users.findOne({ _id: id }) );
}
...
});
where removeSensibleFields is a method to remove all unnecessary/private information that may not be sent back to the client

Categories