Skip to content Skip to sidebar Skip to footer

Sails.js Create Index(root) Controller

I was wondering if there is a way to have an index controller with an index action. my root is a login page and I wanted to detect if the users session is already authenticated and

Solution 1:

You need to make the controller and action yourself. From there, set up a Policy to define access.

To make the controller, run sails generate controller Index in console.

Then, open api/controllers/IndexController.js, make it look something like this:

module.exports = {
  index: function (req, res) {
    // add code to display logged in view
  }
};

Set up config/routes.js to look like this:

module.exports.routes = {
  'get /': 'IndexController.index',
};

Afterwards, define a policy which has your authentication logic. Alternatively, you can use the included session authentication located at api/policies/sessionAuth.js assuming that your login action sets req.session.authenticated = true;. See the docs on policies for more info.

Lastly, connect the policy to the action in config/policies.js:

module.exports.policies = {
  IndexController: {
    '*': false,                  // set as default for IndexController actions
    index: 'sessionAuth'// or the name of your custom policy
  }
}

Post a Comment for "Sails.js Create Index(root) Controller"