42969

Sails.js create Index(root) Controller

Question:

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

Answer1:

You need to make the controller and action yourself. From there, set up a <a href="http://sailsjs.org/documentation/concepts/policies" rel="nofollow">Policy</a> to define access.

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

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

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

Set up <strong>config/routes.js</strong> 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 <strong>api/policies/sessionAuth.js</strong> assuming that your login action sets req.session.authenticated = true;. See <a href="http://sailsjs.org/documentation/concepts/policies" rel="nofollow">the docs on policies</a> for more info.

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

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

Recommend

  • can't get ionic v4 CLI generated component to work
  • Jackson equivalent to @XmlSeeAlso
  • How to extract residuals from curvefit
  • Binding a callback in Backbone.js and Underscore.js
  • gitlab throwing - failed to store ssl session
  • MVC - Is it OK for views to keep reference to their view controller and handle data?
  • Escape syntax for CSS selectors in LESS
  • CSS Drop Down Menu : nav ul ul li Moved to Right
  • Eager Loading with Pagination
  • MySql query to find rooms available given dates 'From' and 'to'
  • How to convert days into months using datetime in Python3?
  • Authorize Attribute Authentication with Postman in Web Api
  • Pick Out Specific Number from Array? [duplicate]
  • Swift manually rotate view controller
  • 'Edit' function for forum posts and such
  • Ajax call on Multiple selection in Select box
  • Year over Year Stats from a Crossfilter Dataset
  • How to write seo friendly url's using htaccess?
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • How to turn off notice reporting in xampp?
  • Android: Unable to detect vertical plane
  • Debug `Unexpected end of JSON input Error` on content script
  • Apple Mach-O Linker error (“duplicate symbol”)
  • Make checkout phone field optional for specific countries in WooCommerce
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • Angular 4: Responsive Grid List
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • Firebase: How to read from external DB?
  • Write to .csv file with PHP (Commas in Data Error)
  • Access to a Matlab gui from the web