Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structuring services in a middleware pattern like in rack and wsgi

Tags:

angularjs

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?

like image 891
murtaza52 Avatar asked Oct 21 '22 21:10

murtaza52


1 Answers

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.

like image 59
ganaraj Avatar answered Nov 04 '22 00:11

ganaraj