I have a brand new Rails api based application, where i need to implement authorization.
Overall Architecture:
React frontend -> Rails API layer -> Rails model/server layer
While exploring different approaches, I have got a confusion.
- Should we put the authorization logic in API layer or Service layer?
- API Layer Approach: We will build some authorization middleware that will sit between our front end and API layer and all our api calls will be routed thorough the authorization middleware to check if the user is allowed to call that parituclar api.
- Service Layer: All the authorization check will go to service layer and we will have check before every db operation if the user is allowed to do so. (Using
cancancan / pundit
) and if the user is not allowed throw the error message to API layer.
It would be a great help, if someone could suggest based on their experience.