Question: 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?

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

Have a look at the official documentation here :

<http://docs.angularjs.org/api/ng.$http>

under the **Response interceptors** section.

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

<http://bl.ocks.org/3410406>

Hope this helps.
[angular-http-auth](https://github.com/witoldsz/angular-http-auth) is already built to do what you are trying to accomplish in AngularJS