How to create route based security policy in Sails js?

Aritrik

I've a Sails js app for 3 user groups, Site admin, Private User & Public user.

Now url pattern are like below for Admin,

'get /admin/foo': 'fooController.viewAll',
'post /admin/foo': 'fooController.add',
'put /admin/foo/:fooID': 'fooController.edit',

Url pattern for authenticated user,

'get /businesses/foo': 'fooController.viewAll',
'post /businesses/foo': 'fooController.add',
'put /businesses/foo/:fooID': 'fooController.edit',

Url pattern for public user,

'get /public/foo': 'fooController.viewAll',

I want to have a policy to authenticate user based on url pattern, if url is like /admin, it'll check user is admin? else if /business it will check whether user is our existing user or not.

taufique

There is a field named originalUrl in request object. You can access it in your policy and decide depending on the url. Policy would be something like following.[I am assuming you know how to define a policy and add it in the configuration.]

module.exports = function(req, res, next) {
        var originalUrl = req.originalUrl;
        var tokenizedOriginalUrl = originalUrl.split('/');
        if(tokenizedOriginalUrl[0] == 'admin'){
            // do something
            next(null);
        }
        else if(tokenizedOriginalUrl[0] == 'business'){
            // do something
            next(null);
        }
        else if(tokenizedOriginalUrl[0] == 'public'){
            // do something
            next(null);
        }
        else{
            next("Access prohibited.");
        }
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sails.js route redirect with a custom policy

Sails JS: How to pass value from policy to controller

Sails.js - How to inject a js file to a specific route?

How to create dynamic policies in sails.js

How to use custom route middleware with Sails.js? (ExpressJS)

Sails JS how to trigger action after automatic route POST

How do I create an AWS ELB listener with frontend security policy? How do I replace the policy?

Why my custom policy is not recognised in Sails JS

Why is my custom policy not recognized in Sails JS?

How to create dynamic form in sails.js with vue.js?

How to create a timer based camel polling route?

How to create Policy-based QoS on Windows 7 through CLI?

Sails JS: How to create model objects from the result of .query()?

How to create a file from render html sails js

how create left join query with sails.js

How to implement content security policy?

How to "avoid" Content Security Policy?

Sails.js filtering records using the route

Sails.js , calling route from Controller

Sails.js dot in route param

How to create a dynamic route in Next.js

How to ensureIndex in sails js

How to setup extra content-security-policy based on file type in koa?

New to Sails.js how can I route to a view without including the layout.ejs?

How Do I Create an "Any Logged-In User" Policy With PostgreSQL Row-Level Security

Content-security-policy with ACE.js

Breeze JS errors with Content Security Policy

How to create a virtual audio output and route it in Ubuntu-based distro

How to override content security policy while including script in browser JS console?