Get current user from inside the model in Sails

I’m using toJSON() method of my model in Sails in order to control the visibility of some of it’s properties, when model is exposed via application’s API.

In order to decide which properties to display and which to omit I need to know the permissions of the current user. So, how do I get the current user from inside the model? Or is there a better way (pattern) to solve this problem?

  • socket hang up error with nodejs
  • Websockets and binary data
  • angular-seed web-script.js and cors
  • how can i render html file in node.js
  • Does node.js support the 'let' statement?
  • Inserting and Querying Date with MongoDB and Nodejs
  • Here’s some sample code I want to achieve:

    toJSON: function () {
    
      var result = {};
    
      result.firstName = this.firstName;
      result.lastName = this.lastName;
    
      // Exposing emails only to admin users.
      if (currentUser.isAdmin()) {
        result.email = this.email;
      }
    
      return result;
    
    }
    

  • Removing a null character from a string in javascript
  • Node.js - why do I get leaks when testing with mocha and zombie?
  • Socket.io custom client ID
  • How to prevent “Error: connect ECONNREFUSED”
  • Two files using supertest with mocha causing EADDRINUSE
  • Intellij Idea warning - “Promise returned is ignored” with aysnc/await
  • One Solution collect form web for “Get current user from inside the model in Sails”

    Your asking about reading a session inside the model call. Currently the way sails and waterline are built you can not do this.

    You can use the select property on your initial model call to restrict the columns returned. Since this would be in the context of your controller you would have access to the req object.

    Here are a bunch of related questions / answers on this topic.

    sails.js Use session param in model

    Is it possible to access a session variable directly in a Model in SailsJS

    https://github.com/balderdashy/waterline/issues/556

    https://github.com/balderdashy/waterline/pull/787

    Sails Google Group Discussion on the topic

    Node.js is the Best Javascript runtime in the world.