user93
user93

Reputation: 1866

Sailjs exposing controller from particular IP address

Is it possible to expose sailjs controller to request from particular IP address without authentication while requiring authentication for request from others

Upvotes: 0

Views: 117

Answers (2)

Kirtan Gajjar
Kirtan Gajjar

Reputation: 88

Yes, You can do it.

Here's how

in your policy.js, add a specific policy on your route/controller method.

'*': ['isSpecificIP']

Now go in api/policies folder and create file called isSpecificIP.js

Add the following code in it -

module.exports = function (req, res, next) {

    if(req.ip == "::ffff:127.0.0.1" || req.ip == "127.0.0.1")
        next();

    else 
        return res.json(401, {err: 'Unauthorized'});
};

The above code allows only request from localhost to pass. The above code will also handle request from IPv6 address.

If it does not work

If you have done this and still the server is not accepting your request, then

  • print your IP address with 'Unauthorized' part like return res.json(401, {err: 'Unauthorized - ' + req.ip});

  • Ensure that you have restarted the server

Upvotes: 1

khushalbokadey
khushalbokadey

Reputation: 1152

Yes. You can create a policy for the route where request for all the ip addresses will be allowed. Inside that policy you can check the ip address using req.ip. After filtering you can use res.redirect() to route specific request to another url.

Upvotes: 0

Related Questions