{"id":13473346,"url":"https://github.com/oauthjs/angular-oauth2","last_synced_at":"2025-12-12T03:09:11.141Z","repository":{"id":25720099,"uuid":"29157050","full_name":"oauthjs/angular-oauth2","owner":"oauthjs","description":"AngularJS OAuth2","archived":false,"fork":false,"pushed_at":"2021-06-22T07:12:21.000Z","size":118,"stargazers_count":595,"open_issues_count":31,"forks_count":187,"subscribers_count":39,"default_branch":"master","last_synced_at":"2025-03-28T11:11:31.539Z","etag":null,"topics":["angular","authentication","oauth2"],"latest_commit_sha":null,"homepage":"","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/oauthjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2015-01-12T21:09:21.000Z","updated_at":"2025-01-20T06:07:52.000Z","dependencies_parsed_at":"2022-09-09T11:51:30.205Z","dependency_job_id":null,"html_url":"https://github.com/oauthjs/angular-oauth2","commit_stats":null,"previous_names":["seegno/angular-oauth2"],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oauthjs%2Fangular-oauth2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oauthjs%2Fangular-oauth2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oauthjs%2Fangular-oauth2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oauthjs%2Fangular-oauth2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oauthjs","download_url":"https://codeload.github.com/oauthjs/angular-oauth2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247174454,"owners_count":20896078,"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":["angular","authentication","oauth2"],"created_at":"2024-07-31T16:01:02.854Z","updated_at":"2025-12-12T03:09:05.849Z","avatar_url":"https://github.com/oauthjs.png","language":"JavaScript","readme":"# angular-oauth2 [![Build Status](https://travis-ci.org/seegno/angular-oauth2.svg)](https://travis-ci.org/seegno/angular-oauth2)\n\nAngularJS OAuth2 authentication module written in ES6.\n\nCurrently `angular-oauth2` only uses the [Resouce Owner Password Credential Grant](https://tools.ietf.org/html/rfc6749#section-4.3), i.e, using a credentials combination (username, password), we'll request an access token (using `grant_type='password'`) which, in case of success, will typically return a response such as:\n\n```\n{\n  \"access_token\": \"foobar\",\n  \"token_type\": \"Bearer\",\n  \"expires_in\": 3600,\n  \"refresh_token\": \"foobiz\"\n}\n```\nInternally we'll automatically store it as a cookie and it will be used in every request adding an `Authorization` header: `Authorization: 'Bearer foobar'`.\n\n---\n\n## Installation\n\nChoose your preferred method:\n\n* Bower: `bower install angular-oauth2`\n* NPM: `npm install --save angular-oauth2`\n* Download: [angular-oauth2](https://raw.github.com/seegno/angular-oauth2/master/dist/angular-oauth2.min.js)\n\n## Usage\n\n###### 1. Download `angular-oauth2` dependencies.\n\n* [angular](https://github.com/angular/bower-angular)\n* [angular-cookies](https://github.com/angular/bower-angular-cookies)\n* [query-string](https://github.com/sindresorhus/query-string)\n\nIf you're using `bower` they will be automatically downloaded upon installing this library.\n\n###### 2. Include `angular-oauth2` and dependencies.\n\n```html\n\u003cscript src=\"\u003cVENDOR_FOLDER\u003e/angular/angular.min.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"\u003cVENDOR_FOLDER\u003e/angular-cookies/angular-cookies.min.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"\u003cVENDOR_FOLDER\u003e/query-string/query-string.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"\u003cVENDOR_FOLDER\u003e/angular-oauth2/dist/angular-oauth2.min.js\"\u003e\u003c/script\u003e\n```\n\n###### 3. Configure `OAuth` (optional) and `OAuthToken` (optional):\n\n```js\nangular.module('myApp', ['angular-oauth2'])\n  .config(['OAuthProvider', function(OAuthProvider) {\n    OAuthProvider.configure({\n      baseUrl: 'https://api.website.com',\n      clientId: 'CLIENT_ID',\n      clientSecret: 'CLIENT_SECRET' // optional\n    });\n  }]);\n```\n\nYou can also configure `OAuth` service in a `.run()` block, in case you retrieve the Oauth server configuration from a ajax request.\n\n```js\nangular.module('myApp', ['angular-oauth2'])\n  .run(['OAuth', function(OAuth) {\n    OAuth.configure({\n      baseUrl: 'https://api.website.com',\n      clientId: 'CLIENT_ID',\n      clientSecret: 'CLIENT_SECRET' // optional\n    });\n  }]);\n```\n\n###### 4. Catch `OAuth` errors and do something with them (optional):\n\n```js\nangular.module('myApp', ['angular-oauth2'])\n  .run(['$rootScope', '$window', 'OAuth', function($rootScope, $window, OAuth) {\n    $rootScope.$on('oauth:error', function(event, rejection) {\n      // Ignore `invalid_grant` error - should be catched on `LoginController`.\n      if ('invalid_grant' === rejection.data.error) {\n        return;\n      }\n\n      // Refresh token when a `invalid_token` error occurs.\n      if ('invalid_token' === rejection.data.error) {\n        return OAuth.getRefreshToken();\n      }\n\n      // Redirect to `/login` with the `error_reason`.\n      return $window.location.href = '/login?error_reason=' + rejection.data.error;\n    });\n  }]);\n```\n\n## API\n\n#### OAuthProvider\n\nConfiguration defaults:\n\n```js\nOAuthProvider.configure({\n  baseUrl: null,\n  clientId: null,\n  clientSecret: null,\n  grantPath: '/oauth2/token',\n  revokePath: '/oauth2/revoke'\n});\n```\n\n#### OAuth\n\nUpdate configuration defaults:\n\n```js\nOAuth.configure({\n  baseUrl: null,\n  clientId: null,\n  clientSecret: null,\n  grantPath: '/oauth2/token',\n  revokePath: '/oauth2/revoke'\n});\n\n```\nCheck authentication status:\n\n```js\n/**\n * Verifies if the `user` is authenticated or not based on the `token`\n * cookie.\n *\n * @return {boolean}\n */\n\nOAuth.isAuthenticated();\n```\n\nGet an access token:\n\n```js\n/**\n * Retrieves the `access_token` and stores the `response.data` on cookies\n * using the `OAuthToken`.\n *\n * @param {object} user - Object with `username` and `password` properties.\n * @param {object} config - Optional configuration object sent to `POST`.\n * @return {promise} A response promise.\n */\n\nOAuth.getAccessToken(user, options);\n```\n\nRefresh access token:\n\n```js\n/**\n * Retrieves the `refresh_token` and stores the `response.data` on cookies\n * using the `OAuthToken`.\n *\n * @return {promise} A response promise.\n */\n\nOAuth.getRefreshToken()\n```\n\nRevoke access token:\n\n```js\n/**\n * Revokes the `token` and removes the stored `token` from cookies\n * using the `OAuthToken`.\n *\n * @return {promise} A response promise.\n */\n\nOAuth.revokeToken()\n```\n\n**NOTE**: An *event* `oauth:error` will be sent everytime a `responseError` is emitted:\n\n* `{ status: 400, data: { error: 'invalid_request' }`\n* `{ status: 400, data: { error: 'invalid_grant' }`\n* `{ status: 401, data: { error: 'invalid_token' }`\n* `{ status: 401, headers: { 'www-authenticate': 'Bearer realm=\"example\"' } }`\n\n#### OAuthTokenProvider\n\n`OAuthTokenProvider` uses [angular-cookies](https://github.com/angular/bower-angular-cookies) to store the cookies. Check the [available options](https://code.angularjs.org/1.4.0/docs/api/ngCookies/service/$cookies).\n\nConfiguration defaults:\n\n```js\nOAuthTokenProvider.configure({\n  name: 'token',\n  options: {\n    secure: true\n  }\n});\n```\n\n#### OAuthToken\n\nIf you want to manage the `token` yourself you can use `OAuthToken` service.\nPlease check the [OAuthToken](https://github.com/seegno/angular-oauth2/blob/master/src/providers/oauth-token-provider.js#L45) source code to see all the available methods.\n\n## Contributing \u0026 Development\n\n#### Contribute\n\nFound a bug or want to suggest something? Take a look first on the current and closed [issues](https://github.com/seegno/angular-oauth2/issues). If it is something new, please [submit an issue](https://github.com/seegno/angular-oauth2/issues/new).\n\n#### Develop\n\nIt will be awesome if you can help us evolve `angular-oauth2`. Want to help?\n\n1. [Fork it](https://github.com/seegno/angular-oauth2).\n2. `npm install`.\n3. Do your magic.\n4. Run the tests: `gulp test`.\n5. Build: `gulp build`\n6. Create a [Pull Request](https://github.com/seegno/angular-oauth2/compare).\n\n*The source files are written in ES6.*\n\n## Reference\n\n* http://tools.ietf.org/html/rfc2617\n* http://tools.ietf.org/html/rfc6749\n* http://tools.ietf.org/html/rfc6750\n* https://tools.ietf.org/html/rfc7009\n","funding_links":[],"categories":["JavaScript","Uncategorized"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foauthjs%2Fangular-oauth2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foauthjs%2Fangular-oauth2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foauthjs%2Fangular-oauth2/lists"}