angular-http-auth-interceptor

1.0.4 • Public • Published

HTTP Auth Interceptor Module for Angular 1.4+

This will :

  • intercept all the requests done in the app before your user logs in
  • broadcasts a message upon reception of a 401 response
  • after login is confirmed, will receive a broadcast and retry the previously pending requests
  • allows you to configure $http for the next requests in your app (for example, you can add a header, like a token for your API)

Usage

Register this module in your app :

var app = angular.module('app', [ 'HttpAuthInterceptor' ]);

And then in your user service :

app.service('UserService', [ '$http', '$cookies', 'AuthInterceptorService', 'ConstantsService',
    function($rootScope, $http, $cookies, AuthInterceptorService, ConstantsService) {
 
    var service = {
                    
        (...)
 
        /*
        * Call this function when the user enters his login and password
        */
    checkLoginAndPassword : function(login, password, rememberMe) {
        $http.post('/user/login', {
            'login' : login,
            'password' : password,
            'rememberMe' : rememberMe
        }).success(function(data, status, headers, config) {
            if (data.success) {
                var xAuthorization = data.xAuthorization;
                AuthInterceptorService.loginConfirmed('success', function(config) {
                    config.headers["X-Authorization"] = xAuthorization;
                    return config;
                });
            }
        }
    }

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i angular-http-auth-interceptor

    Weekly Downloads

    16

    Version

    1.0.4

    License

    none

    Last publish

    Collaborators

    • jarnix