Sails.js create Index(root) Controller

放肆的年华 提交于 2019-12-10 19:53:24

问题


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


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


来源:https://stackoverflow.com/questions/32040634/sails-js-create-indexroot-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!