简体   繁体   中英

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 if so redirect them to another page.

Is there specific notation for how the controller is named? I have already tried IndexController.js and MainController.js. I can't seem to find anything in the documentation about this.

Sails.js Ver: 0.11.0

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
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM