简体   繁体   English

Sails.js创建索引(根)控制器

[英]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. 我的root是一个登录页面,我想检测用户会话是否已经过身份验证,如果是,则将其重定向到另一个页面。

Is there specific notation for how the controller is named? 是否有关于如何命名控制器的具体表示法? I have already tried IndexController.js and MainController.js. 我已经尝试过IndexController.js和MainController.js。 I can't seem to find anything in the documentation about this. 我似乎无法在文档中找到有关此内容的任何内容。

Sails.js Ver: 0.11.0 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. 要制作控制器,请在控制台中运行sails generate controller Index

Then, open api/controllers/IndexController.js , make it look something like this: 然后,打开api / controllers / IndexController.js ,使它看起来像这样:

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

Set up config/routes.js to look like this: config / routes.js设置为如下所示:

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; 或者,您可以使用位于api / policies / sessionAuth.js的包含会话身份验证,假设您的登录操作设置为req.session.authenticated = true; . See the docs on policies for more info. 有关详细信息,请参阅有关策略的文档

Lastly, connect the policy to the action in config/policies.js : 最后,将策略连接到config / policies.js中的操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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