{"id":13777035,"url":"https://github.com/witoldsz/angular-http-auth","last_synced_at":"2025-05-14T13:07:19.733Z","repository":{"id":4289966,"uuid":"5420051","full_name":"witoldsz/angular-http-auth","owner":"witoldsz","description":null,"archived":false,"fork":false,"pushed_at":"2017-10-03T20:59:39.000Z","size":341,"stargazers_count":2369,"open_issues_count":14,"forks_count":412,"subscribers_count":104,"default_branch":"master","last_synced_at":"2025-05-10T09:02:03.236Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/witoldsz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-08-14T23:47:31.000Z","updated_at":"2025-04-17T10:20:31.000Z","dependencies_parsed_at":"2022-09-09T19:00:12.271Z","dependency_job_id":null,"html_url":"https://github.com/witoldsz/angular-http-auth","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/witoldsz%2Fangular-http-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/witoldsz%2Fangular-http-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/witoldsz%2Fangular-http-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/witoldsz%2Fangular-http-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/witoldsz","download_url":"https://codeload.github.com/witoldsz/angular-http-auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254149958,"owners_count":22022851,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T18:00:36.662Z","updated_at":"2025-05-14T13:07:14.721Z","avatar_url":"https://github.com/witoldsz.png","language":"JavaScript","readme":"HTTP Auth Interceptor Module\n============================\nfor AngularJS\n-------------\n\nThis is the implementation of the concept described in\n[Authentication in AngularJS (or similar) based application](http://espeo.eu/blog/authentication-in-angularjs-or-similar-based-application/).\n\nThere are releases for both AngularJS **1.0.x** and **1.2.x**,\nsee [releases](https://github.com/witoldsz/angular-http-auth/releases).\n\nLaunch demo [here](http://witoldsz.github.com/angular-http-auth/)\nor switch to [gh-pages](https://github.com/witoldsz/angular-http-auth/tree/gh-pages)\nbranch for source code of the demo.\n\nUsage\n------\n\n- Install via bower: `bower install --save angular-http-auth`\n- ...or via npm: `npm install --save angular-http-auth`\n- Include as a dependency in your app: `angular.module('myApp', ['http-auth-interceptor'])`\n\nManual\n------\n\nThis module installs $http interceptor and provides the `authService`.\n\nThe $http interceptor does the following:\nthe configuration object (this is the requested URL, payload and parameters)\nof every HTTP 401 response is buffered and everytime it happens, the\n`event:auth-loginRequired` message is broadcasted from $rootScope.\n\nThe `authService` has only 2 methods: `loginConfirmed()` and `loginCancelled()`.\n\nYou are responsible to invoke `loginConfirmed()` after user logs in. You may optionally pass in\na data argument to this method which will be passed on to the loginConfirmed\n$broadcast. This may be useful, for example if you need to pass through details of the user\nthat was logged in. The `authService` will then retry all the requests previously failed due\nto HTTP 401 response.\n\nYou are responsible to invoke `loginCancelled()` when authentication has been invalidated. You may optionally pass in\na data argument to this method which will be passed on to the loginCancelled\n$broadcast. The `authService` will cancel all pending requests previously failed and buffered due\nto HTTP 401 response.\n\nIn the event that a requested resource returns an HTTP 403 response (i.e. the user is\nauthenticated but not authorized to access the resource), the user's request is discarded and\nthe `event:auth-forbidden` message is broadcast from $rootScope.\n\n#### Ignoring the 401 interceptor\n\nSometimes you might not want the interceptor to intercept a request even if one returns 401 or 403. In a case like this you can add `ignoreAuthModule: true` to the request config. A common use case for this would be, for example, a login request which returns 401 if the login credentials are invalid.\n\n### Typical use case:\n\n* somewhere (some service or controller) the: `$http(...).then(function(response) { do-something-with-response })` is invoked,\n* the response of that requests is a **HTTP 401**,\n* `http-auth-interceptor` captures the initial request and broadcasts `event:auth-loginRequired`,\n* your application intercepts this to e.g. show a login dialog:\n * DO NOT REDIRECT anywhere (you can hide your forms), just show login dialog\n* once your application figures out the authentication is OK, call: `authService.loginConfirmed()`,\n* your initial failed request will now be retried and when proper response is finally received,\nthe `function(response) {do-something-with-response}` will fire,\n* your application will continue as nothing had happened.\n\n### Advanced use case:\n\n#### Sending data to listeners:\nYou can supply additional data to observers across your application who are listening for `event:auth-loginConfirmed` and `event:auth-loginCancelled`:\n\n      $scope.$on('event:auth-loginConfirmed', function(event, data){\n      \t$rootScope.isLoggedin = true;\n      \t$log.log(data)\n      });\n\n      $scope.$on('event:auth-loginCancelled', function(event, data){\n        $rootScope.isLoggedin = false;\n        $log.log(data)\n      });\n\nUse the `authService.loginConfirmed([data])` and `authService.loginCancelled([data])` methods to emit data with your login and logout events.\n\n#### Updating [$http(config)](https://docs.angularjs.org/api/ng/service/$http):\nSuccessful login means that the previous request are ready to be fired again, however now that login has occurred certain aspects of the previous requests might need to be modified on the fly. This is particularly important in a token based authentication scheme where an authorization token should be added to the header.\n\nThe `loginConfirmed` method supports the injection of an Updater function that will apply changes to the http config object.\n\n    authService.loginConfirmed([data], [Updater-Function])\n\n    //application of tokens to previously fired requests:\n    var token = response.token;\n\n    authService.loginConfirmed('success', function(config){\n      config.headers[\"Authorization\"] = token;\n      return config;\n    })\n\nThe initial failed request will now be retried, all queued http requests will be recalculated using the Updater-Function.\n\nIt is also possible to stop specific request from being retried, by returning ``false`` from the Updater-Function:\n\n    authService.loginConfirmed('success', function(config){\n      if (shouldSkipRetryOnSuccess(config))\n        return false;\n      return config;\n    })\n","funding_links":[],"categories":["User Manager","JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwitoldsz%2Fangular-http-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwitoldsz%2Fangular-http-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwitoldsz%2Fangular-http-auth/lists"}