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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With