Sails.js 创建索引(根)控制器 [英] Sails.js create Index(root) Controller
问题描述
我想知道是否有办法让索引控制器具有索引操作.我的 root 是一个登录页面,我想检测用户会话是否已经过身份验证,如果是,则将它们重定向到另一个页面.
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.
对于控制器的命名方式是否有特定的表示法?我已经尝试过 IndexController.js 和 MainController.js.我似乎在文档中找不到关于此的任何内容.
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 版本: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.
要制作控制器,在控制台运行sails generate controller Index
.
To make the controller, run sails generate controller Index
in console.
然后,打开api/controllers/IndexController.js,让它看起来像这样:
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
}
};
将 config/routes.js 设置为如下所示:
Set up config/routes.js to look like this:
module.exports.routes = {
'get /': 'IndexController.index',
};
之后,定义一个包含您的身份验证逻辑的策略.或者,您可以使用位于 api/policies/sessionAuth.js 中包含的会话身份验证,假设您的登录操作设置了 req.session.authenticated = true;
.有关详细信息,请参阅政策文档.
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.
最后,将策略连接到 config/policies.js 中的操作:
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
}
}
这篇关于Sails.js 创建索引(根)控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!