Structuring services in a middleware pattern like in rack and wsgi


I have various angular services which call a rest api on the server.

If the user is not authenticated then the server always returns a map — {authenticated: false}, otherwise it returns the expected data.

I would like to create a middleware function which always intercepts the return value. It then sets the $route based on the result. If the user not authenticated then redirects him to a cer tain route.

How do I do it?

Problem courtesy of: murtaza52


Angular’s $http service has responseInterceptors which is probably what you are looking for.

Have a look at the official documentation here :

under the Response interceptors section.

Also another tutorial that explains the response interceptors can be found here.

Hope this helps.

Solution courtesy of: ganaraj

View additional discussion.