I worked with Sails a couple of years ago, I came from Rails, and I have no experience with Node.js.
Now I'm trying to do strong token authentication using jsonwebtoken. https://github.com/auth0/node-jsonwebtoken
I followed this guide http://thesabbir.com/how-to-use-json-web-token-authentication-with-sails-js/ and everything worked fine. I can do registration, log in, and then use the token correctly for different actions.
Now there are some actions in which I would like to use a login user, something like devise current_user helper. For example, when creating a comment, this comment must belong to the current user.
Using the Sabbir Ahmed manual, on line 33 of the isAuthorized.js policy, the token receives the decryption, so I can get the current user ID from it.
So my question is: what should be the best way to get the current user and be able to use it later in some kind of controller? For example, I tried something like:
# isAuthorized.js line 34, after getting decrypted token User.findOne({id: token.id}).exec(function findOneCB(err, found){ currentUser = found; });
But, therefore, since this is an asynchronous action, I cannot use this currentUser in the controller.
I want to save the current user in order to be able to use it later in some controller without repeating the same code in each controller, something like an assistant or, possibly, a service.