This method is used to send a 403 ("Forbidden") response back down to the client indicating that the request is not allowed. This usually means the user-agent tried to do something it was not allowed to do, like change the password of another user.
return res.forbidden();
Or:
return res.forbidden(data);
return res.forbidden(data, pathToView);
Like the other built-in custom response modules, the behavior of this method is customizable.
By default, it works as follows:
data
as JSON. If no data
is provided a default response body will be sent (the string "Forbidden"
).pathToView
was provided, Sails will attempt to use that view.pathToView
was not provided, Sails will serve a default error page (the view located at views/403.ejs
). If that view does not exist, Sails will just send JSON.data
argument will be accessible as a view local: data
.Using the default view:
if ( !req.session.canEditSalesforceLeads ) {
return res.forbidden('Write access required');
}
With a custom view:
if ( !req.session.canEditSalesforceLeads ) {
return res.forbidden(
''Write access required'',
'salesforce/leads/edit'
);
}
- This method is terminal, meaning it is generally the last line of code your app should run for a given request (hence the advisory usage of
return
throughout these docs).res.forbidden()
(like other userland response methods) can be overridden or modified. It runs the response method defined in/responses/forbidden.js
, which is bundled automatically in newly generated Sails apps. If aforbidden.js
response method does not exist in your app, Sails will implicitly use the default behavior.- If
pathToView
refers to a missing view, this method will respond as if the request "wants JSON". +By default, the specified error (err
) will be excluded if the app is running in the "production" environment (i.e.process.env.NODE_ENV === 'production'
).