Book Image

AngularJS Web Application Development Blueprints

By : Vinci J Rufus
Book Image

AngularJS Web Application Development Blueprints

By: Vinci J Rufus

Overview of this book

Table of Contents (17 chapters)
AngularJS Web Application Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Setting up an Interceptor to detect responses


A use case that we need to consider is what happens if the backend web service's session timed out and somebody from the frontend is trying to add, edit, or delete a page.

At the instance when the backend service times out, it would return a 401 status code; we would need to have every AngularJS controller check for this status code and redirect the user to the login page in case it gets one.

Instead of writing this check on each and every controller, we will make use of an Interceptor to check every incoming response, and act accordingly.

Let's chain our Interceptor service in our services.js file as follows:

.factory('myHttpInterceptor', ['$q', '$location', function($q, $location) {
    return {
        response: function(response) {
            return response;
        },
        responseError: function(response) {
            if (response.status === 401) {
                $location.path('/admin/login');
                return $q.reject(response...