{"id":15623703,"url":"https://github.com/justinsa/angular-authentication-service","last_synced_at":"2025-04-28T15:18:28.476Z","repository":{"id":17666302,"uuid":"20470974","full_name":"justinsa/angular-authentication-service","owner":"justinsa","description":"An authentication and authorization helper service for Angular client applications.","archived":false,"fork":false,"pushed_at":"2019-12-19T05:43:54.000Z","size":74,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-28T15:18:23.477Z","etag":null,"topics":["angular","angular-service","authentication","authorization","javascript"],"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/justinsa.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":"2014-06-04T05:11:35.000Z","updated_at":"2022-10-23T19:29:46.000Z","dependencies_parsed_at":"2022-08-31T00:01:03.478Z","dependency_job_id":null,"html_url":"https://github.com/justinsa/angular-authentication-service","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justinsa%2Fangular-authentication-service","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justinsa%2Fangular-authentication-service/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justinsa%2Fangular-authentication-service/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justinsa%2Fangular-authentication-service/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/justinsa","download_url":"https://codeload.github.com/justinsa/angular-authentication-service/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251336391,"owners_count":21573188,"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","angular-service","authentication","authorization","javascript"],"created_at":"2024-10-03T09:58:19.507Z","updated_at":"2025-04-28T15:18:28.453Z","avatar_url":"https://github.com/justinsa.png","language":"JavaScript","readme":"[![Bower Version](https://img.shields.io/bower/v/ng-authentication-service.svg)](https://github.com/justinsa/angular-authentication-service)\n[![NPM Version](https://img.shields.io/npm/v/ng-authentication-service.svg)](https://www.npmjs.com/package/ng-authentication-service)\n![Master Build Status](https://codeship.com/projects/e0e25100-6039-0133-0431-46609ca5f084/status?branch=master)\n[![license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/justinsa/angular-authentication-service/blob/master/LICENSE)\n\nAn authentication and authorization helper service for Angular client applications.\n\n## Dependencies\n\n* AngularJS - http://angularjs.org\n* Lodash - http://lodash.com\n\n## Basic Setup\n\nAdd this module to your app as a dependency:\n```JAVASCRIPT\nvar app = angular.module('yourApp', ['authentication.service']);\n```\n\nInject $authentication as a parameter in declarations that require it:\n```JAVASCRIPT\napp.controller('yourController', function($scope, $authentication){ ... });\n```\n\n## Configuration Options\n\nTo override the default configuration options, configure the module with an options argument during application configuration and provide overrides for any of the following options.\n\n```JAVASCRIPT\napp.config(['$authenticationProvider', function ($authenticationProvider) {\n  $authenticationProvider.configure({\n    authCookieKey: undefined,\n    storageService: undefined,\n    profileStorageKey: '$authentication.user.profile',\n    lastAttemptedUrlStorageKey: '$authentication.last-attempted-url',\n    onLoginRedirectUrl: '/',\n    onLogoutRedirectUrl: '/',\n    notAuthorizedRedirectUrl: '/',\n    notAuthenticatedRedirectUrl: '/',\n    trackLastAttemptedUrl: true,\n    userRolesProperty: 'roles',\n    extensions: undefined,\n    expirationProperty: undefined,\n    events: {\n      loginConfirmed: 'event:auth-loginConfirmed',\n      loginRequired: 'event:auth-loginRequired',\n      logoutConfirmed: 'event:auth-logoutConfirmed',\n      notAuthenticated: 'event:auth-notAuthenticated',\n      notAuthorized: 'event:auth-notAuthorized'\n    },\n    rolesFunction: function (userProfile) {\n      if (_.has(userProfile, this.userRolesProperty)) {\n        var roles = userProfile[this.userRolesProperty];\n        return _.isArray(roles) ? roles : [roles];\n      }\n      return [];\n    },\n    validationFunction: function (userRoles, allowedRoles) {\n      return !_.isEmpty(userRoles) \u0026\u0026 !_.isEmpty(allowedRoles) \u0026\u0026\n        (_.find(allowedRoles, function (role) { return _.includes(userRoles, role); }) !== undefined);\n    },\n    reauthentication: {\n      fn: function () {},\n      timeout: 1200000,\n      timer: undefined\n    }\n  });\n}]);\n```\n\n### Extensions\n\nAll properties (own and inherited) of the extensions object will be available as native to the $authentication service API. The extensions object is applied using the [_.defaults(...)](https://lodash.com/docs/#defaults) method and cannot overwrite any of the existing API properties. This is intended to provide implementors with a way to add objects or functions that are application specific and should fall within the context of the authentication service to expose, e.g., functions to check if a profile has specific roles.\n\n### Storage Service Option\n\nIf you do not provide a storage service then a simple, in-memory dictionary will be used.\n\nYou can provide any storage service or object that supports the following API:\n\n  1. ```any get(key)```\n  2. ```boolean has(key)```\n  3. ```void remove(key)```\n  4. ```void set(key, value)```\n\nTo configure a storage service for the authentication provider you provide the service name:\n\n```JAVASCRIPT\napp.config(['$authenticationProvider', function ($authenticationProvider) {\n  $authenticationProvider.configure({\n    storageService: '$store'\n  });\n}]);\n```\n\nor an object that provides the expected functionality:\n\n```JAVASCRIPT\napp.config(['$authenticationProvider', function ($authenticationProvider) {\n  $authenticationProvider.configure({\n    storageService: new CustomStorageService()\n  });\n}]);\n```\n\nThe ng-authentication-service was designed in tandem with the [ng-local-storage-service](https://github.com/justinsa/angular-local-storage-service).\n\n## API\n\n### isAuthenticated()\n```JAVASCRIPT\n// Returns true if there is a user profile loaded in local storage, false otherwise.\n$authentication.isAuthenticated();\n```\n\n### isAuthCookieMissing()\n```JAVASCRIPT\n// Returns true if the authCookieKey is defined and no auth cookie is present, false otherwise.\n$authentication.isAuthCookieMissing();\n```\n\n### isProfileExpired()\n```JAVASCRIPT\n// Returns true if there is a user profile and it has an expiration value in the past, false otherwise.\n$authentication.isProfileExpired();\n```\n\n### loginConfirmed(data)\n```JAVASCRIPT\n// Store the profile (data) in local storage, notify all listeners of login, and redirect to:\n//   1. lastAttemptedUrl if defined and trackLastAttemptedUrl is true\n//   2. onLoginRedirectUrl if defined\n//   3. do not redirect\n$authentication.loginConfirmed({ ... });\n```\nBroadcast via: ```event:auth-loginConfirmed``` with the ```data``` parameter as an argument.\n\n### checkAndBroadcastLoginConfirmed()\n```JAVASCRIPT\n// Check whether a user is logged in and broadcast the auth-loginConfirmed event, if so.\n$authentication.checkAndBroadcastLoginConfirmed();\n```\n\n### loginRequired()\n```JAVASCRIPT\n// Notify all listeners that authentication is required.\n$authentication.loginRequired();\n```\nBroadcast via: ```event:auth-loginRequired```.\n\n### logoutConfirmed(doNotRedirect)\n```JAVASCRIPT\n// Remove any existing profile from local storage, notify all listeners of logout, and redirect to:\n//   1. do not redirect if @doNotRedirect === true\n//   2. lastAttemptedUrl if defined and trackLastAttemptedUrl is true\n//   3. onLogoutRedirectUrl if defined\n//   4. do not redirect\n$authentication.logoutConfirmed();\n```\nBroadcast via: ```event:auth-logoutConfirmed```.\n\n### allowed(...)\n```JAVASCRIPT\n// Return true if the user is unauthenticated, false otherwise.\n$authentication.allowed('anonymous');\n\n// Return true if the user is authenticated, false otherwise.\n$authenticated.allowed('all');\n\n// Return true if the configured validationFunction returns true, false otherwise.\n$authenticated.allowed('role1', 'role2', ...);\n\n// will flatten provided arrays that are any depth in the arguments list\n$authentication.allowed('X', ['Y', 'Z'], [['A']]) === $authentication.allowed('X', 'Y', 'Z', 'A')\n```\n\n### profile()\n```JAVASCRIPT\n// Get or set the current user profile from local storage.\n// If data is provided then it overwrites the existing user profile before returning it.\n$authentication.profile(data);\n```\n\n### roles()\n```JAVASCRIPT\n// Return the current collection of roles of the user profile from local storage if it exists.\n$authentication.roles();\n```\n\n### isInAllRoles(...)\n```JAVASCRIPT\n// Return true if the current user profile is in all of the specified roles, false otherwise.\n$authentication.isInAllRoles('role1', 'role2', ...);\n\n// will flatten provided arrays that are any depth in the arguments list\n$authentication.isInAllRoles('X', ['Y', 'Z'], [['A']]) === $authentication.isInAllRoles('X', 'Y', 'Z', 'A')\n```\n\n### isInAnyRoles()\n```JAVASCRIPT\n// Return true if the current user profile is in at least one of the specified roles, false otherwise.\n$authentication.isInAnyRoles('role1', 'role2', ...);\n\n// will flatten provided arrays that are any depth in the arguments list\n$authentication.isInAnyRoles('X', ['Y', 'Z'], [['A']]) === $authentication.isInAnyRoles('X', 'Y', 'Z', 'A')\n```\n\n### permit(...)\n```JAVASCRIPT\n// Determine if the current user profile is allowed and redirect to either notAuthorizedRedirectUrl or notAuthenticatedRedirectUrl if not.\n$authentication.permit('role1', 'role2', ...);\n\n// will flatten provided arrays that are any depth in the arguments list\n$authentication.permit('X', ['Y', 'Z'], [['A']]) === $authentication.permit('X', 'Y', 'Z', 'A')\n```\n\n### getConfiguration()\n```JAVASCRIPT\n// Return the configuration object.\n$authentication.getConfiguration();\n```\n\n### getLastAttemptedUrl(fallback)\n```JAVASCRIPT\n// Return the last attempted url value, or fallback if value is undefined or tracking is disabled,.\n$authentication.getLastAttemptedUrl();\n```\n\n### setLastAttemptedUrl(value)\n```JAVASCRIPT\n// Set and return the last attempted url value.\n$authentication.setLastAttemptedUrl();\n```\n\n### store()\n```JAVASCRIPT\n// Returns the configured storage service.\n$authentication.store();\n```\n\n### reauthenticate()\n```JAVASCRIPT\n// Enable re-authentication via the configured reauthentication.fn at reauthentication.timeout intervals.\n$authentication.reauthenticate();\n```\n\n### $onLoginConfirmed(handler)\n```JAVASCRIPT\n// Sets the provided function handler as a listener to the event: 'event:auth-loginConfirmed'.\n// The event data is the data provided to the loginConfirmed call that triggered this event.\n$authentication.$onLoginConfirmed(function (event, data) { ... });\n```\n\n### $onLoginRequired(handler)\n```JAVASCRIPT\n// Sets the provided function handler as a listener to the event: 'event:auth-loginRequired'.\n$authentication.$onLoginRequired(function (event) { ... });\n```\n\n### $onLogoutConfirmed(handler)\n```JAVASCRIPT\n// Sets the provided function handler as a listener to the event: 'event:auth-logoutConfirmed'.\n$authentication.$onLogoutConfirmed(function (event) { ... });\n```\n\n### $onNotAuthenticated(handler)\n```JAVASCRIPT\n// Sets the provided function handler as a listener to the event: 'event:auth-notAuthenticated'.\n// The event data is the array of arguments provided to the permit call that triggered this event.\n$authentication.$onNotAuthenticated(function (event, data) { ... });\n```\n\n### $onNotAuthorized(handler)\n```JAVASCRIPT\n// Sets the provided function handler as a listener to the event: 'event:auth-notAuthorized'.\n// The event data is the array of arguments provided to the permit call that triggered this event.\n$authentication.$onNotAuthorized(function (event, data) { ... });\n```\n\n## Development\nAfter forking you should only have to run ```npm install``` from a command line to get your environment setup.\n\nAfter install you have two gulp commands available to you:\n\n1. ```gulp js:lint```\n2. ```gulp js:test```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustinsa%2Fangular-authentication-service","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjustinsa%2Fangular-authentication-service","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustinsa%2Fangular-authentication-service/lists"}